Cannot get the data in another page in CI - codeigniter

I have form and when i submit that data are passed to next page i can see that in url. but i when i get and print that it shows nothing , here i have attached my coding
<form action="addfilters/add"><input type="hidden" name="id" value=<?php echo $filters->id ?> > <input type="submit" value="Add New"></form>
my controller
<?php
class AddFilters extends Admin_Controller
{
function add(){
echo $this->input->post('id');
}
}
URL
http://localhost/code/index.php/admin/addfilters/add?id=1
but when i print ,it shows nothing , please help me

i have just used
<?php echo form_open_multipart('admin/addfilters/add'); ?>
top of the form. it works fine :-)

Related

CodeIgniter flashdata [flash:old:message] being displayed

I'm using CodeIgniter on OpenShift.
In my controller I'm using:
$this->session->set_flashdata('message', 'message X');
$this->load->view('viewpage');
In my view I'm using:
print_r ($this->session->userdata);
echo $this->session->flashdata('message');
Here are my observations:
first time through the controller/load view, I see nothing echoed with the
$this->session->flashdata('message');
I see this with the print_r:
[flash:new:message]=>message 1
second time through the controller/load view, I see "message 1" being echoed
I see this with the print_r:
[flash:old:message] =>message 1[flash:new:message]=>message 2
So what appears to be happening is that [flash:old:message] is being displayed instead of [flash:new:message]. If [flash:old:message] isn't set, then nothing is displayed.
Please help.
Cheers,
Mike
when you set a value in a flash data, you need to make a view refresh like:
controller.php
function do_somthing(){
$this->session->set_flashdata('index', 'text message');
redirect('controller/view', 'refresh');
}
controller/view.php
<div>
<?= (isset($this->session->flashdata('index'))) ? $this->session->flashdata('index') : ''?>
</div>
Flashdata is designed to be used moving from 1 page to another (redirects), you typically use it after a post, the return a success/failure message.
the reason for this:
[flash:old:message] =>message 1[flash:new:message]=>message 2
occurring is because flashdata is retained for 1 additional page load (so you can use $this->session->keep_flashdata() if required... as you are triggering flashdata by refreshing the page to generate these results its confusing things and not designed for use this way..
This really seems to be an issue occurring due to the way you are using flashdata than it displaying the incorrect data.
A working example of using flashdata is below (even without a redirect)
controller:
public function index()
{
if (!$this->input->post()) {
$this->load->view('playland/index');
}else{
if ($this->input->post('submit') == "submit") {
$data['firstname'] = $this->input->post('firstname');
$data['lastname'] = $this->input->post('lastname');
$this->session->set_flashdata('test', 'data posted');
$this->load->view('playland/retrieve', $data);
}
}
}
index view:
<html>
<body>
<?php print_r($this->session->userdata)?>
<form method="post" action="playland">
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname"><br>
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>
retrieve view:
<html>
<body>
<?php echo $this->session->flashdata('test') ?><br>
<p>
First Name:<br>
<?php echo isset($firstname) ? $firstname : '';?><br>
Last Name:<br>
<?php echo isset($lastname) ? $lastname : '';?><br>
</p>
Click to refresh the page
Return to original page
</body>
</html>

form submit in codeigniter going to blank page

I am submitting form using form helpers and it's going to blank page and when I see the view source, the action in the view source is like this http://::1/ci1/login_validation I don't understand the ::1 in it shouldn't it be localhost:8080/? But if I use simple form tags like regular html it works fine?
<?php
echo form_open ('login_validation');
echo form_input('email');
echo form_password('password');
echo form_submit('login_submit', 'login');
echo form_close();
?>
View Source
<form action="http://::1/ci1/login_validation" method="post" accept-charset="utf-8">
<p>Email:<input type="text" name="email" value="" /></p>
<p>Password:<input type="password" name="password" value="" /></p>
<p><input type="submit" name="login_submit" value="login" /></p>
</form>
I would think it is a couple of thing like.
If your form action has http://::1/ then does not submit correct because base url is empty
$config['base_url'] = '';
Fill base url example:
$config['base_url'] = 'http://localhost/project_name/';
On codeigniter 3 version not best idea to leave it blank because you will run in to that issue.
Make sure your class and file names of controllers etc have first letter upper case.
Filename: Login_validation.php
Controller:
<?php
class Login_validation extends CI_Controller {
public function index() {
// form submit controller code.
}
}
View
<?php
echo form_open ('login_validation');
echo form_input('email');
echo form_password('password');
echo form_submit('login_submit', 'login');
echo form_close();
?>
This is a common mistake people for get to check.
Why http://::1/??
This mean your base_url() is empty.
Then how this http://::1/ comes??
When your project URL is empty, CI detect your project URL. So this http://::1/ knows your project path.
How to set base_url()??
In config/config.php set $config['base_url'] = ''; the project URL.
Keeping base_url() empty any harm??
When you in local its ok and fine. But when you host that just add your site URL to it.
$config['base_url'] = 'www.stackoverflow.com';

Code igniter how to define site title from user input value?

I'm beginner with code igniter
I want to define site title from user input value
I need create a specific Controller for it ?
I think using this code:
view (for build a new page)
Site Title:<input type="text" size="40" name="title" />
view (generated page )
<title><?php echo $title ?> </title>
controller
$title = $_POST['title'];
From what I understood, you may try something like below: But you should read up more on the codeigniter doc.
View File:
<input type="text" name="title">
Controller:
$data['title'] = $this->input->post('title');
$this->load->view('view-file-name', $data);
View File (view-file-name.php)
<title><?php echo $title; ?></title>

Codeigniter submitting form1 form2 on form_open

any idea how to handle this problem ? my problem is when i submit on form2 the form1 automatic submit to. I want is when i submit on form2 only the form2 submit it. Please check this sample code.
<?php echo form_open('form1'); ?>
<input type="text" name="name">
<?php echo form_open('form2'); ?>
<input type="text" name="note">
<input type="submit" value="button2"> // this form2 is to insert note on this day
<?php echo form_close(); ?>
// then under this form2 we have a table. Table for list of note that all ready insert.
<input type="submit" value="button1"> // this form1 is to save all. the "Name" and also the note that we insert it on form2
<?php echo form_close(); ?>
I don't think it is a good way of programming to nest the forms.But after doing bit research for your question found this link which can come to your help.Click here
Hope that will work for you.All the best!
Change your nested submit element to
<input type="submit" name="form2_button" value="form2_submit"/>
Then, you can check the inner form submission using
if(isset($_POST["form2_button"]))` or in codeigniter you can input->post('form2_button')
Having a form nested under another form is not a valid HTML/XHTML. But you can try the follow code above.

Loading page gives an error in CodeIgniter

When I login, it gives me "error page not found". It's not loading the controller function.
This is my view file:
<form name="loginform" action="welcome/login" method="post">
Enter User Name:
<input type="text" name="uname">
Enter Password
<input type="password" name="pass">
<input type="submit" name="login" value="Login">
This is my login controller file:
class Login extends Controller {
function Login()
{
parent::Controller();
}
function login()
{
$this->load->view('welcome_message');
}
}
This is my welcome controller file:
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->model('Loginmodel');
$this->load->view('welcome_message');
}
public function login()
{
$this->load->model('Loginmodel','login');
$info = $this->logn->login();
$this->load->view('welcome_message',$info);
}
}
Can you give me any idea what is wrong?
The code you provided seems fine, so I would guess it has to do with this:
<form name="loginform" action="welcome/login" method="post">
The path is relative, which is only going to work if there are no url segments present. If the current URL is /hello/world, you will end up posting to /hello/world/welcome/login
Try a full URL or absolute path. Example:
<form name="loginform" action="/welcome/login" method="post">
<!-- ^^^ Note the leading forward slash -->
Or use base_url() for the full url:
<form action="<?php echo base_url(); ?>welcome/login">
You may also be interested in the form_open() function which will do this automatically.
<?php echo form_open('welcome/login'); ?>
The only other thing I can see would have to do with whatever $this->login->login(); does. If you can confirm what your URL actually is when you see the "page not found" error, that would be a great help.

Resources