Samuurai
Samuurai

Reputation: 2645

JQuery AJAX post - how to send extra form variables?

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?

Upvotes: 0

Views: 1540

Answers (3)

Sarfraz
Sarfraz

Reputation: 382796

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);
 }
  });    

Upvotes: 3

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103145

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.

Upvotes: 0

Corey Ballou
Corey Ballou

Reputation: 43497

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

Upvotes: 1

Related Questions