Passing JavaScript array to PHP via jQuery $ .ajax

I want to manipulate a JavaScript array in PHP. Is it possible to do something like this?

$.ajax({ type: "POST", url: "tourFinderFunctions.php", data: "activitiesArray="+activities, success: function() { $("#lengthQuestion").fadeOut('slow'); } }); 

Actions are a one-dimensional array, for example:

 var activities = ['Location Zero', 'Location One', 'Location Two']; 

The script does not end when I try to do this ... How to fix it?

+77
javascript jquery ajax php
Jan 06
source share
9 answers
 data: { activitiesArray: activities }, 

What is it! Now you can access it in PHP:

 <?php $myArray = $_REQUEST['activitiesArray']; ?> 
+145
Jan 6
source share

You will need to encode your array as JSON before sending it, or you just get some junk at the other end.

Since all you submit is an array, you can simply do:

 data: { activities: activities } 

which will automatically convert the array for you.

See here for more details.

+11
Jan 06
source share

You need to turn this into a string. You can do this using the stringify method in the JSON2 library.

http://www.json.org/

http://www.json.org/js.html

The code looks something like this:

 var myJSONText = JSON.stringify(myObject); 

So

 ['Location Zero', 'Location One', 'Location Two']; 

It will become:

 "['Location Zero', 'Location One', 'Location Two']" 

You will need to contact your PHP guru on how to handle this on the server. I think the other answers here contain a solution.

Data can be returned from the server in the same way. That is, you can return it back to the object.

 var myObject = JSON.parse(myJSONString); 
+10
Jan 06 '10 at 15:00
source share

I know it might be too late to answer this, but it worked out great for me:

  • Flatten your javascript object (json) with var st = JSON.stringify(your_object);

  • Pass the POST data as a "string" (possibly using jQuery: $.post('foo.php',{data:st},function(data){... });

  • Decode your data when processing on the server side: $data = json_decode($_POST['data']);

So that he ... you can freely use your data.

Multidimensional arrays and single arrays are treated like regular arrays. To access them just follow the usual $foo[4] .

Associative arrays (javsacript objects) are treated as php objects (classes). To access them, just do it as classes: $foo->bar .

+9
Apr 07 '12 at 4:25
source share

I should be like this:

 $.post(submitAddress, { 'yourArrayName' : javaScriptArrayToSubmitToServer }, function(response, status, xhr) { alert("POST returned: \n" + response + "\n\n"); }) 
+2
Nov 19 '14 at 8:22
source share

Use jQuery Serialize Function

http://docs.jquery.com/Ajax/serialize

Serialization is usually used to prepare user data that should be sent to the server. Serialized data are in standard format compatible almost server-side programming languages and frameworks.

+1
Jan 6
source share

This worked for me:

 $.ajax({ url:"../messaging/delete.php", type:"POST", data:{messages:selected}, success:function(data){ if(data === "done"){ } info($("#notification"), data); }, beforeSend:function(){ info($("#notification"),"Deleting "+count+" messages"); }, error:function(jqXHR, textStatus, errorMessage){ error($("#notification"),errorMessage); } }); 

And this is for your PHP :

 $messages = $_POST['messages'] foreach($messages as $msg){ echo $msg; } 
+1
Jun 20 '13 at 17:18
source share

Use the PHP built-in functionality to add an array operand to the name of the desired variable.

If we add values ​​to the Javascript array as follows:

 acitivies.push('Location Zero'); acitivies.push('Location One'); acitivies.push('Location Two'); 

It can be sent to the server as follows:

 $.ajax({ type: 'POST', url: 'tourFinderFunctions.php', 'activities[]': activities success: function() { $('#lengthQuestion').fadeOut('slow'); } }); 

Note the quotes around the action []. Values ​​will be available as follows:

 $_POST['activities'][0] == 'Location Zero'; $_POST['activities'][1] == 'Location One'; $_POST['activities'][2] == 'Location Two'; 
+1
Mar 26 '17 at 20:32
source share

This is because PHP reads your value as a string. If I don't want to transfer my data as an object (as in previous answers, which is also good), I just do it in my PHP:

  $activitiesString = $_POST['activitiesArray']; $activitiesArray = (explode(",",$activitiesString)); 

The last line breaks the line into bits after each comma. Now $ actionsArray is also an array. This works even if there is no comma (only one element in your javascript array).

Good coding!

0
Jul 29 '19 at 7:58
source share



All Articles