Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable data in ajax call JQUERY

Tags:

jquery

ajax

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.

like image 762
fawad Avatar asked Jul 27 '12 11:07

fawad


1 Answers

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

like image 73
Wolfram Avatar answered Sep 21 '22 18:09

Wolfram