3

I'm having an issue with the jquery post() function. I have several buttons and am using a single post function to handle all the buttons. I can't seem to get the variable post name to work.

my code is as follows:

JS

NOTE: the "editor" variable is variable that is set on init of my application and already exists in the scope of this code.

controls.find('button').live('click',function(){

     //determine what button is clicked
     var action  = this.className;

     $.post("actions.php",action: editor);
     //functions run after post
});

the variable doesn't seem to get through. When I change the variable to a string it works fine. I've also tried running the toString(); function on my action variable with no change.

any ideas would be greatly appreciated.

Laurence
  • 409
  • 3
  • 7
  • 17
  • I don't get it, what do you want to do, variables are not sent as action: editor – Senad Meškin Sep 14 '11 at 18:14
  • sorry for the cunfusion. I just need the word 'action to change depending on the button that is clicked and generate the post function with the right action. The action variable is the name of the action and its $_POST value is equal to the editor. make sense. – Laurence Sep 14 '11 at 18:20
  • @Laurence, you can't use a variable to specify the name of a property in object literal syntax. You have to use square bracket notation. See the edit at the end of my answer below. – gilly3 Sep 14 '11 at 18:33

2 Answers2

4

The data variable has to be serialized.

$.post("actions.php",{'action':action,'editor':editor});

If this was a GET, the URL would look like this:
actions.php?action=ACTION_VALUE&editor=EDITOR_VALUE

Jason Kaczmarsky
  • 1,666
  • 1
  • 17
  • 30
  • action needs to be a variable. Quotes make it a string. Right? – Laurence Sep 14 '11 at 18:15
  • Action is a variable? Well you need to have a key for each value. The first element is the key, which is why I assumed it wasn't a variable. See my comment again. – Jason Kaczmarsky Sep 14 '11 at 18:18
  • this works. I was just trying to combine the two values into a single post object. The name of the post being the action and the value being the editor. I can do it like this though. thanks. – Laurence Sep 14 '11 at 18:23
1

This is invalid JavaScript:

action: editor

If editor is a variable defined elsewhere in your code, then maybe you are trying to send an object:

{ action: editor }

Where the object has one property named "action" with the same value as editor. In this case:

  1. The server is sent a post body of "action=[value of editor]".
  2. Your action variable defined earlier is not used, and not sent to the server.

A post body usually takes the form of "key1=value1&key2=value2". jQuery takes an object that is translated to a post body. Such an object would look like { key1: "value1", key2: "value2" }. So your call would look like:

$.post("url.php", { key1: "value1", key2: "value2" });

Or, if you have variables defined for the values:

$.post("url.php", { key1: value1, key2: value2 });

Guessing here... maybe what you want is:

$.post("actions.php", { action: action, editor: editor });

Edit: Based on your comment, maybe this is what you want:

var postData = {};
postData[action] = editor;
$.post("actions.php", postData);

There's no object literal syntax for specifying the name of a property from a variable. You have to do that with square bracket notation as above.

gilly3
  • 87,962
  • 25
  • 144
  • 176