tags:

views:

79

answers:

2

I have a simple form that I wish to postback to the server in an easy fashion and get the result. I am using a custom ISAPI plugin on the backend so using JSON or other funky stuff isn't an option. How best can I do this?

Edit: I would also rather not use external plugins, if possible

+6  A: 

Use serialize to get a string representation of your form and then simply post it using jQuery's post AJAX function.

Very simple example (from jQuery's website using PHP, but any URL would do):

$.post("test.php", $("#testform").serialize());

If you have multiple forms on the page you can use this code on button click to get the correct form Id (where 'someButton' can be any valid jQuery selector):

$('someButton').click(function() {
    //old, less efficient code
    //var formId = $(this).closest("form").attr("id");
    //$.post("test.php", $("#" + formId).serialize());

    //as per Vincent Robert's suggestion, simplified version
    $.post("test.php", $(this).closest("form").serialize());
});
Marek Karbarz
If I have the submit button for a particular form, how do I get the form reference to `serialize()` it?
Chris
see my edit for a suggestion
Marek Karbarz
why not $(this).closest("form").serialize() ?
Vincent Robert
@Vincent - even better
Marek Karbarz
Thanks this worked a treat
Chris
+2  A: 

Another way to do the same thing as Marek commented.

$.ajax({
  type: 'POST',
  url: YOUR_URL,
  data: $('#YOURFORM').serialize(),
  success: function(data) {
    //Probably should do something that shows it worked.
  }
 error: function (xhr, ajaxOptions, thrownError){
    //Log the error if there was one
 }

});
Steven Dorfmeister