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'); } });
Activities is a single dimensional array like:
var activities = ['Location Zero', 'Location One', 'Location Two'];
The script does not complete when I try this... How can I fix it?
on('submit', function (e) { e. preventDefault(); var data = $(this). serializeArray(); var myvals = [21, 52, 13, 24, 75]; // This array could come from anywhere you choose for (i = 0; i < myvals. length; i++) { data.
You could use JSON. stringify(array) to encode your array in JavaScript, and then use $array=json_decode($_POST['jsondata']); in your PHP script to retrieve it. don't forget to sanitize your inputs, or you're toast! :D.
An Array is used to store multiple values in a single variable. This can be used to pass the group of related values as data to the $. ajax for processing and get the response. E.g. pass all checked checkboxes values, selected values from the list.
data: { activitiesArray: activities },
That's it! Now you can access it in PHP:
<?php $myArray = $_REQUEST['activitiesArray']; ?>
You'll want to encode your array as JSON before sending it, or you'll just get some junk on the other end.
Since all you're sending is the array, you can just do:
data: { activities: activities }
which will automatically convert the array for you.
See here for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With