Home > Article > Backend Development > How to Pass a JavaScript Array to PHP Using jQuery AJAX?
Passing JavaScript Array to PHP with jQuery AJAX
When attempting to manipulate a JavaScript array in PHP, it's crucial to establish a proper communication channel between the two languages. One effective method to achieve this is through the use of jQuery's $.ajax() function.
To successfully pass a JavaScript array to PHP via jQuery AJAX, the data parameter requires a slightly different approach. Instead of directly assigning the array to the data, it should be wrapped within an object notation:
data: { activitiesArray: activities },
Now, when the AJAX request is sent, it will properly format and encode the array within the HTTP payload.
On the PHP side, you can then access the array using $_REQUEST:
<?php $myArray = $_REQUEST['activitiesArray']; ?>
This enables you to seamlessly manipulate the JavaScript array within your PHP code.
The above is the detailed content of How to Pass a JavaScript Array to PHP Using jQuery AJAX?. For more information, please follow other related articles on the PHP Chinese website!