I'm using CodeIgniter and trying to redirect back to the controller with a "status update" after an action url has been run... I can explain this better.. So I have an admin page "example.com/admin" and I manage users at "/admin/users" and When i delete a user it goes to "*/admin/delete_user*". I want it to redirect BACK to "/admin/users" and add a variable set like $status_update with a success or failure message.
How could I get a message back to the other view? Here's my simplified code.
*My full code is actually full of security measures so ignore it's lack of currently.
class Admin extends CI_Controller {
public function index(){
$this->load->view('admin/admin');
}
public function users(){ // admin/users
$this->load->model('user_model');
$users = $this->user_model->get();
$this->load->view('admin/users', ['users' => $users]);
}
public function delete_user($user_id){ // admin/delete_user
$this->load->model('user_model');
if ($this->user_model->delete($user_id) == true){
$this->load->model('user_model');
$users = $this->user_model->get();
$this->load->view('admin/users', ['users' => $users]);
// I WANT THIS TO GO BACK TO "/admin/users" WITH THE STATEMENT BELOW
echo $user_id . ' has been deleted from the system';
} else {
echo 'Something went wrong trying to delete ' . $user_id;
}
}
}
How could I go about this? The only way I can see about going something like this is by moving the delete function into the users function and call it with a form submission variable but I feel like there might be a better way.
I hope my question makes sense.
Update
Thanks to tomexsans & saurabh2836 I ended up solving this with a small addition to the end
I moved the actual call into the view itself because of complications with sending an additional array into the view from the controller wasn't going well but I also had a small issue where the flash data was lasting through the request and then through a second which was causing problems and I fixed that with the following
<?php if ($this->session->flashdata('message') !== null){
$_session_data = $this->session->flashdata('message');
$message = $_session_data['message'];
$status = $_session_data['status'];
if ($status == 'success'){
echo '<div class="alert-success"><strong>'.$message.'</strong></div>';
} elseif ($status == 'failed'){
echo '<div class="alert-danger"><strong>'.$message.'</strong></div>';
}
$this->session->unset_userdata('flash_message'); //this fixed data showing after another request.
} ?>
Update 2
For future users looking to use this method I found a strange happenstance where if you make the request with a tag it won't display as you'd think (I have a feeling it has something to do with browser caching) so to fix this you have to make calls through a form. This is how I implemented my method.
<form action="<?=site_url("admin/delete_user/{$_value->user_id}")?>" method="post">
<button type="submit">Delete</button>
</form>
I know it's a bit much compared to a link but I don't want to rely Javascript to submit a form. Have another idea?