Hello I am new to codeigniter. I am learning this framework by developing a testing application. I am showing user listing and in front of each record an anchor tag to edit that record. That anchor tag looks like
echo(anchor('first/edit_user/'.$value->id,'Edit'));
It redirects the browser to first controller and edit_user function. At edit_user function I load the view of edit like this.
$this->load->view('edit_user',$data);
It loads the view with respect to selected record to edit the record and the url looks like
http://localhost/CodeIgniter/index.php/first/edit_user/9
Every thing works fine. But when user clicks the update button it again comes to same function. It updates record and then again tries to load the same view but here a problem occurs. Now after updating record it loads the view and url becomes like this
http://localhost/CodeIgniter/index.php/first/edit_user
It creates error as it is not having the id of the selected record. IF i change the load view code of my function like开发者_开发问答 this
$this->load->view('edit_user/'$this->uri->segment(3)),$data);
It generates an error of edit_user/.php is not defined and some thing like that. Now I want to ask How can I Redirect the user to same edit form telling that his record has been updated? How to load the view of selected record from function of the controller?
To redirect to the same URL as the current one:
redirect(current_url());
Otherwise, be specific where you want to redirect.
$this->load->view('edit_user/'$this->uri->segment(3)),$data);
It generates an error of edit_user/.php is not defined and some thing like that.
Instead of using specific URL segments, use the parameters passed to your controller and set defaults, as well as handle the possible absence or invalidity of the arguments passed (url segments). Example:
function edit_user($id = NULL)
{
if ( ! $id) // handle error (redirect with message probably)
$user = $this->user_model->get($id);
if ( ! $user) // User not found, handle the error
// If user found, load the view and data
}
Remember that controllers are still just php classes and functions, and accept user input (anything I can type in the address bar) - so never assume anything about what's in the URL, always respond appropriately if the data you need isn't there.
First load the URL helper "config/autoload.php"
/*
| -------------------------------------------------------------------
| Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('url');
Or load the URL helper in your function itself
public function edit_user($user_id = '')
{
if(!is_numeric($user_id) {
// user id is not here redirect somewhere //
redirect('home/index');
}
if(isset($_POST['submit'])) {
// proceed to model // @param is optional // you can pass hidden field from form also //
$this->user_model->edit_user($user_id)
}
$this->load->helper('url');
$this->load->view('first/edit_user', $data);
}
Now go to your form :
<form method="post" action="<?php echo current_url(); ?>">
</form>
This will return to same edit page, after submit also.
Hope this helps you, let us know if anything there... Thanks!!
精彩评论