Variable data in ajax call JQUERY

If you want to have a variable-variable in your POST request, you will need to make a seperate JSON object:

var name = 'next';
var dataObject = {};
dataObject[name] = 1;

$.ajax({
   type: "POST",
   url: "somephp.php",
   data: dataObject,
   complete : function() {
     // success!
   }
});

It should be

$.ajax({
   type: "POST",
   url: "somephp.php",
   data: {"data":$move},
});

If you want to use the variable as the name of the property, use array notation.

There are two ways to access object members: dot notation and bracket notation (a.k.a. subscript operator).

Your code with array notation:

var $move = 'next';
var data = {};
data[$move] = 1;

$.ajax({
   type: "POST",
   url: "somephp.php",
   data: data,
});

The example on jsfiddle (the post obviously doesn't work, so check the console to see what gets posted.)

Tags:

Ajax

Jquery