I currently have an issue with my URI’s
Example I have a function called add – users/add if there is an error with my input form it displays the errors on the users/add page but if all is good it goes back to the users page and shows a success but the uri remains /users/add and I have to re navigate to the users page.
Update:
add:
$data['message'] = '<div class="alert alert-success"><strong>Thank You</strong> Your User Has Been Added</div>';
$this->session->set_userdata('message', $data['message']);
redirect(site_url('dashboard/users'));
users:
$message = $this->session->userdata('message');
if($message === FALSE)
{
$message='';
}else{
$this->session->unset_userdata('message');
}
Original:
Controller:
public function add()
{
$this->form_validation->set_rules('userFirstName','First Name', 'required|trim|max_length[99]|xss_clean');
$this->form_validation->set_rules('userLastName','Last Name', 'required|trim|max_length[99]|xss_clean');
$this->form_validation->set_rules('userEmail','E-Mail', 'required|valid_email|trim|max_length[99]|xss_clean|is_unique[users.email]');
$this->form_validation->set_rules('userPassword','Password', 'required|trim|max_length[99]|xss_clean');
if($this->form_validation->run() === TRUE)
{
$userData = array(
'fName' => $this->input->post('userFirstName', TRUE),
'lName' => $this->input->post('userLastName', TRUE),
'email' => $this->input->post('userEmail', TRUE),
'password' => sha1($this->input->post('userPassword', TRUE))
);
$this->db->escape($userData);
$this->user_model->addUser($userData);
$data['contentMangement'] = $this->options_model->systemOptions();
$data['pageTitle'] = 'Add User';
$data['message'] = '<div class="alert alert-success"><strong>Thank You</strong> Your User Has Been Added</div>';
$this->load->view('_assets/dashHeader', $data);
$this->load->view('dashboard/users', $data);
$this->load->view('_assets/footer');
}elseif($this->form_validation->run() === FALSE)
{
$data['contentMangement'] = $this->options_model->systemOptions();
$data['pageTitle'] = 'Add User';
$data['message'] = validation_errors('<div class="alert alert-error">', '</div>');
$this->load->view('_assets/dashHeader', $data);
$this->load->view('dashboard/addUser', $data);
$this->load->view('_assets/footer');
}
}
This happens because you are not redirecting away from your current page. You are merely loading the ‘users’ view from
users/add.Think of it this way: The page you are at is
users/add. What content this page shows is determined by which view(s) you load. So, even though you load theuserview, you’re still at the pageusers/add.If you want to redirect, instead of
You need to do
However, in the latter case, you cannot pass data as you do when you load a view. If you want to retain data after a redirect, you will need to store it in session.
So, in your above code, where you create $data[‘message’], add it to the session:
When you redirect, at the other controller, retrieve the message, and remove it from session:
EDIT
In your edit, in the
userscontroller, you are extracting the message from session. But, it’s not available to the view. You still need to pass it on.