Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: post a multidimensional array

I am learning how to use jQuery, $.post and php. (I am not a pro like you guys)

I want to send a multidimensional array to php.
My array looks something like this:

var item= new Array();
item[0] = ["Object", "Value"];
item[1] = ["id", "x"];
item[2] = ["status", "y"];
item[3] = ["date", "z"];
etc...

This is my jQuery code:

//AJAX
$("#add").click(function()
{
$.post( 'ajax_new.php' ,
    {
    item : item
    },

    function(data)
    {
    alert( data );
    } //end: if:else

); //END:$.post
}); //END:ajax

Also, after posting the array, how do I handle it in php?
Like this?:

<?
$id = $_POST['item'][1][1];
echo $id;
?>
like image 761
Omar Avatar asked Dec 05 '25 14:12

Omar


1 Answers

I use to convert collected data to JSON before send it to the server. http://api.jquery.com/serializeArray/

like image 152
Fran Diéguez Avatar answered Dec 07 '25 02:12

Fran Diéguez