Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

2 Answers

up vote 6 down vote accepted

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); }
});
share|improve this answer
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. – Daniel Apr 26 '11 at 20:20
@mkeats. First is correct, second not. Try this: var i=0;console.log(i++);console.log(i);. It will log 0 and 1; – jerone Apr 26 '11 at 20:20
@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. – jerone Apr 26 '11 at 20:27
@Daniel, JSON.stringify([{name:'Dude'},{name:'Guy',phone:234556}]) yields "[{"name":"Dude"},{"name":"Guy","phone":234556}]" – Ustaman Sangat Apr 16 '12 at 23:15

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'];
...
share|improve this answer
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. – Daniel Apr 26 '11 at 20:09
You want to convert a javascript array to a Json string? – slandau Apr 26 '11 at 20:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.