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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With