tags:

views:

101

answers:

3

Hi,

I'm using ajax for the first time and have hit a bit of a brick wall.

I've created a single big function with lots of smaller functions inside it which handles all ajax requests. I'm passing a POST variable to tell it which function to process the request with.

For example

if(_is_ajax()) {
  if($_POST['save_company']) {
  // Do stuff here
  }
}

However, I don't know how to use jQuery ajax functions in a way where I can set the post variable AND pass serialized data.

here's my code:

var values = $("#edit_company").serialize();
$.post("/admin/companies", { 'save_company' : true } ,
 function(data){
  console.log(data);
 }, "json")

'save_company' : true sets the post variable, but where do I include values?

+1  A: 
$.post("/admin/companies", { 'save_company' : 'value1', var2: 'value2', var3: true }, callbackFcn);
cballou
+3  A: 

You can try something similar to this:

var frm_data = $("#form1_id").serialize();

 $.ajax(
 {
 type: "POST",
 url: "url.php",
 data: frm_data,
 cache: false,

 success: function(response)
 {
   alert(response);
 }
  });
Sarfraz
@Sarfraz's serialized answer is probably closer to what you were looking for, you would simply need to add a hidden input field for **save_company** in the form.
cballou
@cballou: that will be fine :)
Sarfraz
+1 cballou - the hidden field is cleaner.
David Robbins
Silly me.. - I was trying to make it too complex in my head.. a simple hidden input field.. durrThanks everyone!
Samuurai
A: 

Assuming that your form is #edit_company then:

 $.post("/admin/companies", $("#edit_company").serialize() ,
    function(data){
            console.log(data);
    }, "json");

Should send all the form data.

Vincent Ramdhanie