Codeigniter从URL传递值
Function receives departure date and adult from view page ( it passes value correctly as I have checked doing echo function ) , but from this function I am not able to pass values to another function
public function index()
{
if ($data = $this->input->post('muktinath')) {
$date = $data['departure_on'];
$adult = $data['adult'];
$getNameValue = array(
$date = 'departure_on',
$adult = 'adult',
);
redirect('booking/muktinath/' . $getNameValue);
}
else{
$this->load->view('index');
}
}
This is the function which must receive the value
public function muktinath($getNameValue)
{
echo $getNameValue;
// here value must be shown of departure date and adult passed from above
}
You didn't share the error messages you are getting. Bet there several.
The biggest issue is you cannot put an array
as a part of your query string. But the fix is pretty easy.
function index()
{
$data = $this->input->post('muktinath');
if($data)
{
//From your question $data seems to be an array already. Just use that!
redirect('booking/muktinath/'.$data['departure_on'].'/'.$data['adult']);
}
else
{
$this->load->view('index');
}
}
Your function that receives the values is then defined this way.
public function muktinath($date, $name)
{
echo $date . " - " . $name;
}
You will have a problem if $date
contains any slashes (/) in what I assume is a date string. The slashes will become part of the URL which, if the date was "7/9/2016" and name was "sandesh" would make this URL.
http://example.com/booking/muktinath/7/9/2016/sandesh
As you can see, it has several more URI segments than you are expecting. This URL would echo "7 - 9" which is not very helpful. You might have to adjust the format of the date while sending it and then reformat it after it is received by muktinath()
.
By the way, your array declaration is wrong. I think you were trying to make this.
$getNameValue = array(
'departure_on' => $date,
'adult' => $adult
);
The thing is, you have just recreated what $data
already was. But you don't need $getNameValue
anyway.
Your problem is this:
redirect('booking/muktinath/' . $getNameValue);
Try echoing out booking/muktinath/'.$getNameValue and you will see you have a array to string error. A redirect is a url, but you have got the data from a post. You cannot easily convert from a post to a url and you should not do so either, otherwise what was the point of posting the data in the first place. How do you know the post values do not have illegal url characters in them?
Why redirect at all, just post your form to the correct page in the first place and deal with the post data there?
Either validate, format for url and then use the actual components in your url call NOT like this but something like this....
redirect('booking/muktinath/'.$date.'/'.$adult);
Or better, do not redirect here but call a function (a library or a model or in your controller, that is up to you)
public function muktinath($date, $adult)
{
....
return TRUE/FALSE or whatever you need.
}
And in your controller check the return value to see what to do
if ($this->mylibraryorwhatever->mukinath($date, $adult)
{
// success
redirect('success page'); // or whatever
}
else
{
// fail
.....
}
Hope that helps.