Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameter to the redirect method in URL helper in Codeigniter?

Tags:

codeigniter

I'm new to Codeigniter, and I want redirect to a controller after the validation is fall:

if(!validate)
{
  redirect('/poll/list');
}

but I need to pass a variable like $error to show the some error indication, but I don't know how to pass the parameter to redirect method in the URL helper, and idea?

like image 855
mko Avatar asked Oct 13 '12 09:10

mko


People also ask

Can we pass data in redirect CodeIgniter?

You can then pass $message along to the View for being displayed above the form. And that's it. User submits the form, the form gets processed by another Controller method, the latter stores the message in the session flashdata and redirects the user to the original Controller method that loads the form.

Can we pass data in redirect?

Yes. If not passing the object, then passing the id so that the other function can retrieve it from the database.


2 Answers

Use session flashdata - this is exactly what it is designed for:

if(!validate)
{
  $this->session->set_flashdata('error', 'your_error');
  redirect('/poll/list');
}

Then inside your poll/list function:

$error_msg = $this->session->flashdata('error');
like image 160
Laurence Avatar answered Oct 13 '22 13:10

Laurence


base url = 'http://localhost/site/'

URL http://localhost/site/controller/method

$this->uri->segment(1) = 'controller'
$this->uri->segment(2) = 'method'

Now check the below case also

base url = 'http://testsite/test/site/'

URL http://testsite/test/site/controller/method

$this->uri->segment(1) = 'controller'
$this->uri->segment(2) = 'method'

Pass your message

http://testsite/test/site/controller/method/meesage

and use $this->uri->segment(3)

You can use session also instead passing message through URL..

like image 30
mrsrinivas Avatar answered Oct 13 '22 11:10

mrsrinivas