I'm creating php system using codeigniter framework. I'm using basic framework without any templates. When I submit data from html page to controller to save to db after redirecting with the error message to view file, i can't remove the url that i used to save data in controller file. How I solve this problem?
This is the form i used to send data to controller.
<form role="form" action="save_user_type" method="POST" >
<div class="row">
<div class="form-group col-md-6">
<label>User Type</label>
<input type="text" class="form-control" name="type" id="type" placeholder="Enter ...">
</div>
</div>
<div class="row">
<div class="form-group col-md-6"></div>
<div class="form-group col-md-6">
<button type="submit" class="btn default">Save</button> <button type="button" class="btn default">Reset</button>
</div>
</div>
</form>
This is the controller function i called.
function save_user_type(){
try {
$this->load->model('Configuration/Configuration_model');
if($this->Configuration_model->save_types() == 1){
$success['done'] = "Good Job";
}else{
$success['dont'] = "Error";
}
$this->load->view('template/header');
$this->load->view('template/menubar');
$this->load->view('configuration/user/index',$success);
} catch(Exception $exc){
return $exc->getTraceAsString();
}
}
Please Help me to solve this problem.
You will need to redirect to a new controller/function in order to get the URL to change in the browser. I'm going to call the controller control
and add a couple functions to handle success and failure.
public function save_user_type(){
try {
$this->load->model('Configuration/Configuration_model');
if($this->Configuration_model->save_types() == 1){
redirect('control/save_success');
}else{
redirect('control/save_fail');
}
} catch(Exception $exc){
return $exc->getTraceAsString();
}
}
public function save_sucess(){
$this->load->view('template/header');
$this->load->view('template/menubar');
$this->load->view('configuration/user/index');
}
public function save_fail(){
$this->load->view('template/header');
$this->load->view('template/menubar');
//the "fail" view will have the error message
//and maybe a link back to the page with the form.
$this->load->view('configuration/user/fail');
}
Another option for presenting a failed attempt is to use sessions to pass an error message back to the controller/function that loads the form and redirect to that page instead of the save_fail
page.
I think you need to redirect to your index page after submitting your data. You may please use this instead of loading view page.
redirect('controller_name/function_name');
use flashdata to show messages and redirect to another page.
function save_user_type(){
try {
$this->load->library('session');
$this->load->model('Configuration/Configuration_model');
if($this->Configuration_model->save_types() == 1){
$this->session->set_flashdata('success',"Good Job");
redirect('controller_name/function_name');
}else{
$this->session->set_flashdata('error',"Error Occured");
redirect('controller_name/function_name');
}
$this->load->view('template/header');
$this->load->view('template/menubar');
$this->load->view('configuration/user/index',$success);
} catch(Exception $exc){
return $exc->getTraceAsString();
}
}
On view to show message use $this->session->flashdata('success');
for success and $this->session->flashdata('error');
for error