Reputation: 4003
This is simple I guess but I am new to codeigniter, so please help. I have a form page that takes data and when submitted stores that data in database. I then tried two ways to redirect back to main page:
first: $this->load->view('home');
second: redirect('/login/form/', 'refresh');
They successfully redirect or load the home page, but url is still /property/new_property which is the view for data input. also when I click refresh on the home page with url as stated above, data is resubmitted so multiple records in database. How can I redirect to home and make the url property/home?
Thanks
Upvotes: 2
Views: 7977
Reputation: 1
i have faced same issue with codeigniter 3.1.7 , i have just simply validate the submit button value with name , example as below
in view side
<input type="submit" value="save" name = "submit">
in controller side
if(isset($_POST['submit'])){}
Upvotes: 0
Reputation: 4686
You need url helper to use
redirect('/home')
This is the correct way to redirect at CI. http://ellislab.com/codeigniter/user_guide/helpers/url_helper.html
To load that helper you can use $this->load->helper('url');
or to write it inside your autoload.php at config folder (this way it will be loaded to all page).
For example here is one simple submition page at my site.
public function save_tips(){
if($this->input->post('save_tips')){
//Form validation and contact with model
$res = $this->model->save_tips();
if(count($res['errors'])==0){ //no errors means success
redirect('/my_tips');
} else{
$errors = $res['errors'];
}
}
$data = array();
if(isset($errors)) {
$data["errors"] = $errors;
}
$this->load->view('save_tips',$data);
}
Upvotes: 0
Reputation: 11
$this->session->set_flashdata('message', 'New Post has been added');
redirect('somefunction', 'refresh')
set_flashdata after save successful will help you to destroy your submitted data and redirect will change your url.
Upvotes: 1
Reputation: 5411
$this->load->view()
will only load the view and the url will not change .
When you use redirect()
it will redirect to a different page and your url will change.
And for your solution You can select the data which you are going to insert in your model before inserting and if it exists then don't run the insert query simply redirect to a different page.
Upvotes: 0