11

I am trying to do something fairly simple but I can't seem to find the solution. I want to post a multi-dimensional array to a php page using jQuery's .ajax function, but I can't seem to serialize the array properly.

The code is as follows

var dataToSend = new Array();
  dataToSend["page"] = location.href;
  dataToSend["data"] = new Array();
  var dataindex = 0;
  jQuery(".myclass").each(function(){
      dataToSend["data"][dataindex]=new Array();
      dataToSend["data"][dataindex]["selector"] = unique_selector(jQuery(this), "");
      dataToSend["data"][dataindex]["contents"] = jQuery(dataToSend["data"][dataindex]["selector"]).html();
  });
  jQuery.ajax({
      type: 'POST',
      url: "/main/save.php",
      data: JSON.stringify(dataToSend),
      dataType: "json",
      success: function(data){alert(data);}
  });

basically I am not sure how to properly pass the dataToSend array. Right now firebug show the post as empty even though the array is loaded with all kinds of good stuff.

Thanks,

Daniel

3 Answers 3

12

You're defining new Array();, but you're using them as new Object(). Try using objects.

Try this:

var dataToSend = { 
    page: location.href, 
    data: []
};
var dataindex = 0;
jQuery(".myclass").each(function(){
    var temp = unique_selector(jQuery(this), "");
    dataToSend.data[dataindex++] = {
        selector: temp,
        contents: jQuery(temp).html()
    };
});
jQuery.ajax({
    type: 'POST',
    url: "/main/save.php",
    data: JSON.stringify(dataToSend),
    dataType: "json",
    success: function(data){ alert(data); }
});
Sign up to request clarification or add additional context in comments.

3 Comments

I take it that there is not equivalent of JSON.stringify for arrays? Anyway that did work now to sort out the php side. Thank you very much.
@mkeats. No worries. @Daniel. Array's are integer indexed lists. Object's are key indexed lists. JSON need to have perfect JavaScript variables, or it won't convert.
@Daniel, JSON.stringify([{name:'Dude'},{name:'Guy',phone:234556}]) yields "[{"name":"Dude"},{"name":"Guy","phone":234556}]"
1

Use

data: { '': dataToSend }

I used this in a similar scenario and it worked like charm...

Comments

0

Taken from the PHP help pages:

you may have multidimensional array in form inputs

HTML Example:

<input name="data[User][firstname]" type="text" />
<input name="data[User][lastname]" type="text" />
...

Inside php script after submit you can access the individual element like so:

$firstname = $_POST['data']['User']['firstname'];
...

2 Comments

Yes I saw this option, but I am not posting the contents of a form. It is a javascript array that I assembled. I am in need of a way to convert that array to a json string.
You want to convert a javascript array to a Json string?

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.