CodeIgniter Forums
How to return user to previous page with the same url - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to return user to previous page with the same url (/showthread.php?tid=56857)



How to return user to previous page with the same url - El Forum - 01-25-2013

[eluser]razputin[/eluser]
Let's say I have a url like example.com/articles/view/1234

On this page users can submit a comment, how can I return them to the same page/url on either success or failure?

Currently I have a function in the controller add_comment, and when they submit the comment it takes them to example.com/articles/add-comment and I want it to be the same as the url they submitted the comment on.

For now I'm cheating to redirect them to the same page with:
Code:
function add_comment()
  {
  $this->load->library('form_validation');
  // field name, error message, validation rules
  $this->form_validation->set_rules('comment', 'Comment', 'trim|required|min_length[2]|xss_clean');
  if($this->form_validation->run() == FALSE)
  {
   $this->view(1234);
  }

So how do I redirect them to the previous page and retain the original url?


How to return user to previous page with the same url - El Forum - 01-25-2013

[eluser]razputin[/eluser]
I've added this in the view

Code:
echo form_open("articles/add_comment/".$article['id']."");

and change this in the controller

Code:
function add_comment($article_id)
  {
  $this->load->library('form_validation');
  // field name, error message, validation rules
  $this->form_validation->set_rules('comment', 'Comment', 'trim|required|min_length[2]|xss_clean');
  if($this->form_validation->run() == FALSE)
  {
   $this->view($article_id);
  }

but it still changes the url into example.com/articles/add-comment


How to return user to previous page with the same url - El Forum - 01-25-2013

[eluser]Harold Villacorte[/eluser]
Try:

Code:
redirect(current_url());

But the way you are doing it it normal, you do not get the parameter when you reload the view after form validation run. But you can repopulate the form with:

Code:
<input type="text" name="field_name" value="<?php echo set_value('field_name', 'defualt_value');?>" />



How to return user to previous page with the same url - El Forum - 01-26-2013

[eluser]InsiteFX[/eluser]
I save the url in the users session and then upon logging in redirect back to the page that they last clicked. You can use the same idea, save to the users session in the string you want to redirect to, and also what data. Then in your controller that will handle the redirect, use both pieces of information in the Controller to handle it.

Here is an example:
Code:
$this->session->set_userdata('redirect_uri', 'after_redirect_controller'); //What page to go to after redirect controller
$this->session->set_userdata($data);
redirect('redirect_controller');

//Redirect Controller
function index()
{
  $redirect_uri = $this->session->userdata('redirect_uri');
  
  //Do something with other variables from session here

   redirect($redirect_uri);
}



How to return user to previous page with the same url - El Forum - 01-26-2013

[eluser]razputin[/eluser]
Thanks for the responses, for now I'm using this code:

Code:
redirect('articles/view/'.$article_id);

seems to be working and it also prevents form resubmission