1

I'm trying to send some data in an Array via AJAX to save it to the database, I build the array this way:

$( "#saveordering" ).button().click(function( event ) {
            event.preventDefault();

            var data = document.getElementById('tabs');
            var categories = data.getElementsByTagName("div");
            var categoryArray = new Array();

            for (var i=0; i < categories.length; i++) { //Loop door de categoriëen
                var category = categories[i];
                var categoryId = category.getAttribute('id');

                categoryArray[i] = new Array();

                categoryArray[i]['id'] = categoryId;
                categoryArray[i]['forums'] = new Array();

                var forums = category.getElementsByTagName("li");
                for (var j=0; j < forums.length; j++) { //Loop door de forums
                    var forum = forums[j];
                    var forumId = forum.getAttribute('id');
                    categoryArray[i]['forums'][j] = new Array();
                    categoryArray[i]['forums'][j]['id'] = forumId;
                }
            }
            $.ajax({
                type: 'POST',
                url: "ajax/updateboardorder.php",
                dataType: 'json',
                data: {ldelim}"categories" : categoryArray{rdelim} ,
                success: function(data) {
                }
            }); 
        });

But nothing is send, when I do a var_dump($_POST) in PHP I'm getting:

array (size=0) empty

What am I doing wrong?

웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
Kaj
  • 2,445
  • 3
  • 23
  • 34
  • `data: {ldelim}"categories" : categoryArray{rdelim}` <- does that look valid to you ? – adeneo May 09 '13 at 18:41
  • @adeneo maybe its symbolic for curly braces – Lee Meador May 09 '13 at 18:41
  • @adeneo Yes {ldelim} and {rdelim} will be replaced by { and } , it's because I'm using the Smarty framework – Kaj May 09 '13 at 18:42
  • @adeneo that is the smarty syntax. It is valid – karthikr May 09 '13 at 18:42
  • The next question would have to be; are you sure your javascript gets parsed by PHP. And you should'nt be using smarty templates in the ajax function, thinking the will get converted on the serverside, it does'nt work that way, the javascript would have to be parsed before it's even outputted to the browser. – adeneo May 09 '13 at 18:44
  • I'd trust it more if the JS was taken from a debugger in a browser. I'd also be interested in what that same debugger said was in the POST request sent by the AJAX call. – Lee Meador May 09 '13 at 18:45
  • My curious mind is also wondering why all the other curly braces show up as curly braces and just those two show up as {ldelim}, etc. – Lee Meador May 09 '13 at 18:47
  • I think some type at {ldelim}? – S P May 09 '13 at 18:47
  • If you look at the answers as of this point in time, nobody knows how you want the data to be formatted in the POST request. You could have it as a block of JSON, a single 'param' with a block of JSON, some huge number of separate 'param's that get parsed on the server side. What do you want it to be? – Lee Meador May 09 '13 at 18:56
  • The first thing I'd do is certainly replacing the smarty crap with : `data: {categories : categoryArray},`, and then maybe read up a little on the difference between arrays and objects in javascript, which can be confusing if you're used to PHP. – adeneo May 09 '13 at 18:59
  • @adeneo If its a simple array `['a', 'b', 'c']` that will generate something PHP will read as an array. Like `cat=a&cat=b&cat=c` or with `[]` after the array name. What happens when the array elements are complex objects containing arrays? – Lee Meador May 09 '13 at 19:07
  • @adeneo Yes I will place the javascript in an external js file, this was just easier to test and with smarty I need to use {ldelim} and {rdelim} if the opening and closing brace are on the same line – Kaj May 09 '13 at 19:24
  • 1
    @LeeMeador - from my experience jQuery has no problem with complicated objects as long they are valid and properly structured. jQuery does convert the objects to strings anyway, but I have never had an issue with this, and I've sent the strangest things with ajax. – adeneo May 09 '13 at 19:24
  • @LeeMeador I wanted an array of the categories (with their ID in it) and an array with the forums+id's that are in those categories. Succeeded to do this at the way described in the answer below with json_decode($_POST['categories'],true) – Kaj May 09 '13 at 19:24
  • You don't have to put those opening and closing braces on the same line. That part could easily be on three lines. – Lee Meador May 09 '13 at 19:25
  • 1
    This is related: http://stackoverflow.com/questions/8698770/javascript-array-to-php-with-jquery-ajax?rq=1 and be sure to follow the "dupe" link at the top and read that one too. – Lee Meador May 09 '13 at 19:30

2 Answers2

4

Look at this code

categoryArray[i] = new Array();
categoryArray[i]['id'] = categoryId;
categoryArray[i]['forums'**strong text**

Um, that is not an "array", you are making an associative array

categoryArray[i] = {};
categoryArray[i]['id'] = categoryId;
categoryArray[i]['forums'] = {};

or

categoryArray[i] = {
    "id" : categoryId,
    "forums" : {}
};

You want an object. Same where you do it later in the code with forums.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • But the array will still get built, and should be sent to the server, even if an object is what should be used ? – adeneo May 09 '13 at 18:47
  • 1
    @adeneo But using an array and `['id']` will set a property, and won't be sent in the request – Ian May 09 '13 at 18:50
-1

This is what's going on to your array:

var a = new Array();
a['id'] = 123;

JSON.stringify(a); // []
a; // []
a.length; // 0

a.id; // 123

You are trying to use the array like a primitive object:

var o = {};
o.id = 123;

JSON.stringify(o); // {"id":123}
Rick Viscomi
  • 8,180
  • 4
  • 35
  • 50