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?

16
  • data: {ldelim}"categories" : categoryArray{rdelim} <- does that look valid to you ? Commented May 9, 2013 at 18:41
  • @adeneo maybe its symbolic for curly braces Commented May 9, 2013 at 18:41
  • @adeneo Yes {ldelim} and {rdelim} will be replaced by { and } , it's because I'm using the Smarty framework Commented May 9, 2013 at 18:42
  • 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. Commented May 9, 2013 at 19:24
  • 1
    This is related: stackoverflow.com/questions/8698770/… and be sure to follow the "dupe" link at the top and read that one too. Commented May 9, 2013 at 19:30

2 Answers 2

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.

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

2 Comments

But the array will still get built, and should be sent to the server, even if an object is what should be used ?
@adeneo But using an array and ['id'] will set a property, and won't be sent in the request
-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}

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.