I can successfully post an array to PHP using Ajax Post but I want to extend this functionality to post an array of objects instead an array of values..im struggling to find clear examples of how to do this.
Here is how I am posting the array of values from the jQuery/Javascript client script:
var placesfortrip = {};
placesfortrip["place"+counter] = inputVal;
//note counter is an int that gets incremented for each value
//inputVal is a string with the new value to be added
var inputVal = jQuery("#edit-location").val();
jQuery.ajax({
url: "/createtrips/updateitin",
type: 'POST',
data: placesfortrip,
dataType: 'json'
});
Then at the PHP side I read the data like this:
$citynames = array();
foreach ($_POST as $key=>$value) {
$citynames[$key]= $value;
}
How can I modify the above to post an array of objects instead of values?
Im particularly confused as to how to read the array of objects from the PHP side.
You can try this code to send your data to php file
then replace this line
with
for other help you can also help this one
Most of the popular JavaScript frameworks have JSON utility functions included. For instance, jQuery has a function that directly calls a url and loads the JSON result as an object : http://docs.jquery.com/Getjson
However, you can get an open-source JSON parser and stringifier from the json website :
https://github.com/douglascrockford/JSON-js/blob/master/json2.js
Then, simply include the code and use the JSON.stringify() method on your array.