I have the following code:
name= "a";
value="b"
$.post("ajax.php", {name:value})....
However ajax.php
will receive name=b
instead of a=b
.
How would I make it receive the latter?
I have the following code:
name= "a";
value="b"
$.post("ajax.php", {name:value})....
However ajax.php
will receive name=b
instead of a=b
.
How would I make it receive the latter?
Share Improve this question edited Nov 26, 2011 at 19:02 pimvdb 155k80 gold badges311 silver badges356 bronze badges asked Nov 26, 2011 at 18:04 TarangTarang 76k39 gold badges219 silver badges279 bronze badges6 Answers
Reset to default 8var obj = {};
obj[name] = value;
$.post("ajax.php", obj)...
To get the effect you desire you can do the following...
name = "a";
value = "b";
var values = {};
values[name] = value;
$.post("ajax.php", values);
The name here is not treated as to be replaceable ...
you should manually write :
{a:"b"}
name= "a";
value="b";
var o = {};
o[name]= value;
$.post("ajax.php", o);
You can't use a variable for key element (if you consider using literal declaration). It has to be static
You need to do this:
$.post("ajax.php", {"a":value})
You may be able to build the object programmably though. You can create an object in this way:
var obj = {};
obj["a"] = "b";
So try this:
var obj = {};
obj[name] = value;
And pass that to the post call...