How can I pass an array in the url in javascript?
I tried this :
http://codeigniter/index.php/AssistanceMonitoringModule/assistanceMonitoring/getReport?remarks="+stat+"&sortBy="+sortBy+"&fromDate="+fromDate+"&toDate="+toDate+"&area="+area;
which the remarks is the one being catch in PHP as an array and stat is the javascript array being supplied in the remarks. Is there any solution for that?
whenever a time catch the remarks in PHP which I use $this->input->get_post('remarks') (since im using codeigniter) i got an error 'Invalid argument supplied for foreach()'
Thans for your help. :)
-
get_post implies you're expecting a POST method? At any rate, you can encode an array for php like so: array[]=x&array[]=y...and so on.Sacho– Sacho2014年05月19日 01:48:57 +00:00Commented May 19, 2014 at 1:48
2 Answers 2
You can encode the array as JSON, with JSON.stringify and decode it in PHP with json_decode. Don't forget to use encodeURIComponent for the values in the URL, otherwise you might not get the URL you expected.
1 Comment
Try like this.
http://codeigniter/index.php/AssistanceMonitoringModule/assistanceMonitoring/getReport?get[remarks]="+stat+"&get[sortBy]="+sortBy+"&get[fromDate]="+fromDate+"&get[toDate]="+toDate+"&get[area]="+area;
foreach($_GET['get'] as $get){
....
}
3 Comments
foreach($_GET as $value){. There is no need to only use a single query parameter.remark as array. I.e. stat is an array. He doesn't want to make every parameter an element of an array.