Using object in the jQuery post parameter

I'm thinking this is not possible but I would like some insight as to why. I want to do something like this:

var pVals = {
    ob1: "postvar1",
    ob2: "postvar2",
    ob3: "postvar2"

$.post("php/dosomething.php",{pVals.ob1:"object 1", pVals.ob2:"object 2", pVals.ob3:"object 3"});

I get an error along the lines of:

missing : after property id

Thanks for the help.


You can get the same end result, you just can't do it in one statement:

var pVals = {
    ob1: "postvar1",
    ob2: "postvar2",
    ob3: "postvar2"

var obj = {};
obj[pVals.ob1] = "object 1";
obj[pVals.ob2] = "object 2";
obj[pVals.ob3] = "object 3";
$.post("php/dosomething.php", obj);

This is because the "keys" in the anonymous hash style constructor are only treated as literals, they can't be expressions themselves. But when you use the array-like referencing, you can use any valid javascript expression to calculate the "key".

I don't necessarily know how to do it exactly how you want it there. You could however send the object and json_decode() it on the php side.


$.post("php/dosomething.php",{pNames:pNames, pValues:pVals});


$p_names = json_decode($_POST['pNames']);
$p_vals = json_decode($_POST['pValues']);

   $$p_names[$i] = $p_vals[$i];

Should have the same effect that you're looking for.

Need Your Help

Lexicographic sorting in F#


I am playing with a toy problem (Convex hull identification) and needed lexicographic sorting twice already. One of the cases was given a list of type Point = { X: float; Y: float }, I would like t...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.