Welcome Guest, Not a member yet? Register   Sign In
Form_validation == false, reload class
#1

[eluser]mojitoo[/eluser]
Hey!

I've written a forum where the users can answer to a thread. But I want check if the users have written something in the answer box. My problem is how I shall write to redirect the users back to the correct address since it contains a thread id like this:
www.example.com/show_thread/3424de3

The code works just fine if I write $this->show(); but the problem is then that the user leaves the thread. So I tried to do a redirect instead but that didn't work. So how shall I write to reload the method show_thread with an id?

Code:
function add_answer() {

  $thread_id = $this->session->userdata('thread_id');

  $this->load->library('form_validation');
  $this->form_validation->set_rules('comment', 'Comment', 'trim|required');

  if($this->form_validation->run() == FALSE) {

   //echo "fail";
   //$this->show();
   redirect('show_thread/'.$thread_id);
  } else {

   //echo "success";
   $this->Thread_model->save_comment();

  }
}
#2

[eluser]Stefan Hueg[/eluser]
You have two options.

Either:
Upon requesting the thread page, put a variable like "last_url" in the users session where you store the current URL, like:

Code:
$this->session->set_userdata('last_url', $this->uri->uri_string());
and redirect with:
Code:
redirect($this->session->userdata('last_url'));

Or:
Code:
redirect($_SERVER['HTTP_REFERER']); //unsafe

I hope that helps.




Theme © iAndrew 2016 - Forum software by © MyBB