views:

79

answers:

1

Hello,

Am wondering how to post an array using $.ajax. My array is something like this:

var a = new Array();
a['test'] = 1;
a['test2'] = 2;
and so on...

I tried:

$.ajax({
  url: baseUrl+"chat.php",
  data: { vars: a},
  type: 'post',
  success: function(data) {
alert(data);
}});

Any suggestions? Thank you for your time.

+3  A: 

Try this one:

var a = {};
a['test'] = 1;
a['test2'] = 2;

// or


var a = {};
a.test = 1;
a.test2 = 2;

// or


var a = {
    test : 1,
    test2 : 2
};

$.ajax({
  url: baseUrl+"chat.php",
  data: a,
  type: 'post',
  success: function(data) {
    alert(data);
  }
});

You may then access the data in your PHP script like this:

$_POST['test'];
$_POST['test2'];
Ionuț G. Stan
doesnt seem to work :(
Alec Smart
oh it working. my bad. thanks a ton! :)
Alec Smart
No problem. I'm glad it worked out eventually.
Ionuț G. Stan