By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,501 Members | 1,263 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,501 IT Pros & Developers. It's quick & easy.

Getting arguments with AJAX

mikek12004
100+
P: 200
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
Jul 20 '09 #1
Share this Question
Share on Google+
8 Replies


acoder
Expert Mod 15k+
P: 16,027
You could pass a string with delimiters and split on the server-side.
Jul 20 '09 #2

mikek12004
100+
P: 200
So it is impossible to use an array eh?
Jul 20 '09 #3

acoder
Expert Mod 15k+
P: 16,027
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.
Jul 20 '09 #4

Canabeez
100+
P: 126
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. ;)
Jul 22 '09 #5

gits
Expert Mod 5K+
P: 5,390
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 ...
Jul 23 '09 #6

Canabeez
100+
P: 126
@gits
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.
Jul 23 '09 #7

gits
Expert Mod 5K+
P: 5,390
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 :)
Jul 23 '09 #8

Canabeez
100+
P: 126
True....... True......... ;)
Jul 23 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.