9

I'm trying to use variable in AJAX call in jquery but it is not working. move variable contains different values. Please check the following code:

var $move = 'next';

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

Suggest any way to use $move variable in data.

fawad
  • 1,323
  • 11
  • 31
  • 50
  • data accepts key value pairs. For now you your $move is acting as key and that is why it is not working. Make it a value and it should work.. Like data: { "data" : $move} – AdityaParab Jul 27 '12 at 11:55
  • 1
    I think what the user is asking is how to pass something different for the key part of the value pair. – Kris.Mitchell Jul 27 '12 at 11:58
  • 1
    What if he actually wants to use the variable as a key? – Subir Kumar Sao Jul 27 '12 at 11:59
  • If your problem is that the variable should be used as property name, and not its value, please see [my answer](http://stackoverflow.com/a/11687303/95033). – Wolfram Jul 27 '12 at 11:59
  • Why would anyone want to have "dynamic" keys? I am not getting any idea why is that necessary. – AdityaParab Jul 27 '12 at 12:01
  • 1
    It allows to use variables and [characters that are not allowed in dot notation](http://stackoverflow.com/a/4968448/95033). There are definitely examples where you want this. For example, I experienced this once with a jQuery UI dialog, for which I needed dynamic multilanguage button labels. – Wolfram Jul 27 '12 at 12:12
  • Thanks Wolf. I never came across such scenario so was just wondering... :) – AdityaParab Jul 27 '12 at 12:36

7 Answers7

14

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.)

Wolfram
  • 8,044
  • 3
  • 45
  • 66
8

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!
   }
});
Sem
  • 4,477
  • 4
  • 33
  • 52
  • 1
    I had to figure this out with a lot of research, so for reference, when the `data` is a PlainObject and not a String, using `complete:function(response)` returns a PlainObject. For example, `alert(response)` returns `[object Object]` and `console.log(response)` returns `Object {readyState: 4, responseText: "example", status: 200, statusText: "OK"}`. So to retrieve `responseText`, use `success:function(response)`. –  Jan 05 '16 at 19:36
3

It should be

$.ajax({
   type: "POST",
   url: "somephp.php",
   data: {"data":$move},
});
AdityaParab
  • 7,024
  • 3
  • 27
  • 40
2

You can do something like data: $move+"=1"

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
1

I was looking for something like this. I wanted to have a variable for the key AND a variable for the value.

let dataPair = {};
dataPair[dataKey] = dataValue;
$.ajax({
  url: TheAPIPath,
  data: dataPair,
  method: "GET",
(etc)
John Grabauskas
  • 310
  • 2
  • 5
0

To post the value of $move variable, do this:

$.ajax({
   type: "POST",
   url: "somephp.php",
   data: {move: $move}
});
dexter.ba
  • 292
  • 2
  • 5
-1

It looks like you are trying to use a PHP variable in javascript. You can use .serialize to gather data and pass it to the ajax call. But as for making calls with different named variables for the name of the value pair, you will need to gather that information from the php page you are passing.

Example:

$.get("jQueryFunctions.php?action=checkCaptula",$('#resetPasswordDiv :input').serialize()).done(function(data){ ....

While this is a .get instead of a .ajax, it's just shorthand for the .ajax call. The passwordDiv contains HTML inputs with names and id. It gathers the information and passes it to the php page for processing.

Kris.Mitchell
  • 998
  • 4
  • 17
  • 34