views:

98

answers:

1

I've done some work with Scriptaculous, and now I'm dipping my feet in to JQuery to see which of the two frameworks I like working with the most. I am running in to some behavior that strikes me as odd...

     $.ajax({
  type: "POST",
  url: "addcart.php",
  data: "userID="+ userID + "&cardID=" + cardID + "&cardQTY=" + cardQTY + "&set=m10",
  success: function(msg){alert("succeed " + msg)},
  error: function(msg){alert("fail " + msg)}
 });

I try to keep my variable names as similar as possible from page to page, so I was attempting to pass "variable name = variable value" to my PHP. However, this call kept passing "foo=foo&bar=bar insead of "userID=foo&cardID=bar", which led to quite a bit of confusion until I noticed what was going on.

I suppose my question is, did I somehow trigger some bug, or is this an intentional feature of JQuery? I can see it coming handy when trying to do "$('#variable')" if it's intentional...

Edit to add:

On the recieving end everything seemed to be working fine. Just the variable names were not what I expected since it was passing the variable value=variable value instead of name=value

When I changed the code to the below the function worked as I intended it I assume because the names are not used as variables anywhere:

         $.ajax({
  type: "POST",
  url: "addcart.php",
  data: "UID="+ userID + "&CID=" + cardID + "&QTY=" + cardQTY + "&set=m10",
  success: function(msg){alert("succeed " + msg)},
  error: function(msg){alert("fail " + msg)}
 });

I prefer to keep the variable names the same, however, so I presume the answer below will work for me?

+1  A: 

It's not a bug, it's a feature! JQuery is trying to make your life easier by letting you pass parameter data as object properties instead of fiddling about with string manipulation and encodings.

data: {
    userID: userID,
    cardID: cardID,
    cardQTY: cardQTY,
    set: 'm10'
}

It does seem to support passing a custom query string though, are you sure it's being properly constructed?

Alex Barrett