views:

114

answers:

3

Following on from my previous question, which was so quickly answered by Meder it wasn't funny, there's now an additional question that's popped up in the process of making a reusable jQuery form submitted that doesn't take the user away from where they were.

Problem

The jQuery serialize() function is performing its magic on all forms within a page, and not the specific form which was submitted. Example code below.

How do I capture the form's unique name/id, and replace "form" within $("form").serialize() with the name of the target form so only that is serialised?

Form code

<form name="contact" id="contact" action="">
  <input name="_recipients" type="hidden" value="[email protected]" />
  <input name="_subject" type="hidden" value="Title" />
  ...
    <fieldset>
      <label for="name" id="name_label">Name</label>
      <input type="text" name="name" id="name" size="30" value="" class="text-input" />
      <label class="error" for="name" id="name_error">This field is required.</label><br />

      <label for="email" id="email_label">Return Email</label>
      <input type="text" name="email" id="email" size="30" value="" class="text-input" />
      <label class="error" for="email" id="email_error">This field is required.</label><br />

      <label for="phone" id="phone_label">Return Phone</label>
      <input type="text" name="phone" id="phone" size="30" value="" class="text-input" />
      <label class="error" for="phone" id="phone_error">This field is required.</label><br/>

        <br />
      <input type="submit" name="submit" class="button" id="submit_btn" value="Send your suggestion" />
    </fieldset>
  </form>

jQuery serialise and post

    var dataString = $("form").serialize();
    //alert (dataString);return false;

    $.ajax({
      type: "POST",
      url: "/_global/assets/scripts/formmail/formmail.asp",
      data: dataString,
...
+1  A: 
var dataString = $('#contact').serialize()

If you are attaching an event handler to a button or the form's submit event then you can refer to it with this if inside the submit event handler function scope, eg

$('#contact').submit(function() {
 alert( $(this).serialize() );
});

I highly recommend reading http://docs.jquery.com/Selectors

meder
Thanks for the quick response, however my whole point of this is not having to specify the name of the field in the actual code - otherwise has to be changed each time it is reused. Needing to detect the name of the form being passed and adding this in-place of form programatically.
thewinchester
Did you see my second code snippet regarding setting up a submit event handler? You dont have to specify if you use the `this` keyword in the correct function scope/context.
meder
You can do something more generical like `$('form.validate').submit(function(){})` and then use `this` inside to refer to the form.
meder
Example: http://jsbin.com/oyoyo
meder
Thank you, I will now eat my own words and must once again bow to your immense expeirence.Check out the sample link above, makes it pretty clear what needs to be done.Summary, apply a unique class to the form, detect for this class when submit button is activated, and tell serialize() only to process data for the form in question.
thewinchester
A: 
$("#contact").serialize()

Instead of using serialize there's a nice plugin that allows you to post your forms asynchronously. All you have to do is:

$(function() {)
    $('#contact').ajaxForm(); 
});

Also don't forget to assign the correct action to the form, it shouldn't be empty.

Darin Dimitrov
Thanks for the quick response, however my whole point of this is not having to specify the name of the field in the actual code - otherwise has to be changed each time it is reused. Needing to detect the name of the form being passed and adding this in-place of form programatically.
thewinchester
+1  A: 

By using the "form" string as a selector, you are actually getting all the FORM elements within the page, in order to select only one form, you can:

Get the specific form by its id attribute (using the id selector):

var dataString = $("#contact").serialize();

Or by its name attribute (using the attributeEquals selector):

var dataString = $("form[name=contact]").serialize();
CMS
Thanks for the quick response, however my whole point of this is not having to specify the name of the field in the actual code - otherwise has to be changed each time it is reused. Needing to detect the name of the form being passed and adding this in-place of form programatically.
thewinchester