0

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.

0

3 Answers 3

1

You can try this code to send your data to php file

 var status  = document.getElementsByName("status")[0];
    var jsonObj = []; //declare array

    for (var i = 0; i < status.options.length; i++) {
        jsonObj.push({id: status.options[i].text, optionValue: status.options[i].value});
    }

then replace this line

data: placesfortrip,

with

data: jsonObj,

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi thanks for the example, it sounds like the key is passing a string representation of the array of objects in the correct format. My XHR post seems to show a query string (but with the square brackets being passed as hex values) and the XHR post tab in Firebug seems to show the values as being objarray1[object1] = <<value>> objarray1[object2] = <<value>>..etc which looks okay..so my issue might be in how I am getting the values in PHP..so far does it look like the right format is being posted in terms of json
0

You should be able to post the object to the server, and use json_decode($_POST['some_key"]) to use that as an array/object on the server

Comments

0

You could also just forget json and post the array as it is. E.g. in JavaScript:

var slideShowImages = new Array();
var image = {};
image['id'] = 1;
image['title'] = "Title 1";
slideShowImages[0] = image;

etc. filling in the whole array in javascript. Then the Ajax call as:

$.ajax({
  type : 'POST',
  url: BASE_REQUEST_URL + 'apiModule/performAddEditSlideShow',
  cache: false,
  data: {
    images: slideShowImages
  }
});

Then in PHP, simply loop through it:

$images = $_POST['images'];
foreach($images as $image) {
  $title = $image['title'];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.