Connecting Tech Pros Worldwide Forums | Help | Site Map

Getting arguments with AJAX

mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 187
#1: Jul 20 '09
Is it possible to put an array of values as an argument to a js function? I want through this function and with AJAX to pass this array to a PHP array inside the PHP script which will then do the rest

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Jul 20 '09

re: Getting arguments with AJAX


You could pass a string with delimiters and split on the server-side.
mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 187
#3: Jul 20 '09

re: Getting arguments with AJAX


So it is impossible to use an array eh?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Jul 20 '09

re: Getting arguments with AJAX


Well, you could use an array, but it'd be passed as a string, e.g.
[1,2,3] would be passed as "1,2,3". You could serialize it on the client-side and deserialize it on the server-side.
Canabeez's Avatar
Member
 
Join Date: Jul 2009
Location: Israel
Posts: 85
#5: Jul 22 '09

re: Getting arguments with AJAX


Try something like:

Expand|Select|Wrap|Line Numbers
  1. var myPostVars = "myArray[0]=AAA&myArray[1]=BBB";
  2.  
then in PHP the $_REQUEST["myArray"] should already be a compiled array.

Expand|Select|Wrap|Line Numbers
  1. var_dump($_REQUEST["myArray"]);
  2.  
Not sure it will work, well it did in PHP3, but things have changed since then, but still worth trying. ;)
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,134
#6: Jul 23 '09

re: Getting arguments with AJAX


that suggestion shows a 'kind of serializing' a javascript array ... since you cannot use that easyly in both languages ... a very seamless way to handle more complex datatypes is to use JSON ... so that you might use simple eval() on JS-side and json_encode() or json_decode() in php ... that will do the serializing and deserializing for you ...
Canabeez's Avatar
Member
 
Join Date: Jul 2009
Location: Israel
Posts: 85
#7: Jul 23 '09

re: Getting arguments with AJAX


Quote:

Originally Posted by gits View Post

that suggestion shows a 'kind of serializing' a javascript array ... since you cannot use that easyly in both languages ... a very seamless way to handle more complex datatypes is to use JSON ... so that you might use simple eval() on JS-side and json_encode() or json_decode() in php ... that will do the serializing and deserializing for you ...

You're right, I'd prefer building an XML in JS and then parse it in PHP, think that would be the right way to do it, or JSON as you mentioned. But the question was about passing an array through an AJAX request.
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,134
#8: Jul 23 '09

re: Getting arguments with AJAX


JSON is more lightweight compared to XML ... so i would always prefer JSON ... and the other point is: what you have suggested is a serialized array that is passed as a string ... and no array ... to be correct :)
Canabeez's Avatar
Member
 
Join Date: Jul 2009
Location: Israel
Posts: 85
#9: Jul 23 '09

re: Getting arguments with AJAX


True....... True......... ;)
Reply