Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8299953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:21:36+00:00 2026-06-08T16:21:36+00:00

I started out with this code for submitting a form using the jQuery ajax

  • 0

I started out with this code for submitting a form using the jQuery ajax method (which is working without issue)

$( function() {
//bind an event handler to the submit event for the appointment form
    $("#idw-form").on("submit", function(e) {
//cache the form element for use in this function
    var $form = $( this );
//prevent the default submission of the form
    e.preventDefault();
//run an AJAX post request to your server-side script, $this.serialize() is the data from your form being added to the request
    $.post($form.attr('action'), $form.serialize(), function (responseData) {
        if (responseData == 1) {
            $.mobile.changePage( 'thank-you-dialog.html', { transition: "pop" } );
        } else {
            $.mobile.changePage( 'error-dialog.html', { transition: "pop" } );
        }
        return false;
    });
});
});

Then I wanted to introduce validation using the jQuery validate plugin. I thought I should be able to just put this code into the submitHandler callback function, but it wasn’t working. Spent hours working it over, debugging in firebug and finally got it working. There were a couple of things that were causing the problem.

  1. I had to comment out the e.preventDefault(); that I’m used to needing. (now that I’m typing this, I’m vaguely remembering that maybe I only included this line of code because that was needed in order to get it working with jQuery Mobile Framework, but I’m not sure).
  2. I had to change the variable that was being .serialize()‘d. For some reason when the form was submitted with this code, the serialize function would always return an empty string.

Here is my revised code that I was finally able to get working:

submitHandler: function(form) {
    formObj = $( '#' + form.id );
    var replaceDiv = $( '#main-content' );
    //prevent the default submission of the form
    //e.preventDefault();
    //run an AJAX post request to your server-side script, $this.serialize() is the data from your form being added to the request
    $.ajax({
        type: 'POST',
        url: '/scripts/idw-mail-handler.php',
        dataType: 'json',
        data: formObj.serialize(),
        success: function(responseData, responseStatus, responseXml){
            console.log( 'responseData is: ' + responseData );
            console.log( 'responseStatus is: ' + responseStatus );
            console.log( 'responseXML is: ' + responseXml );
            replaceDiv.hide();
            if (responseData.sentStatus == 1) {
                replaceDiv.before(responseData.successMessage);
                $('html, body').animate({
                    scrollTop: 0
                }, 300);
                console.log( 'sentStatus equals: ' + responseData.sentStatus );
            } else {
                replaceDiv.before(responseData.errorMessage);
                $('html, body').animate({
                    scrollTop: 0
                }, 300);
                console.log( 'sentStatus equals something other than 1: ' + responseData.sentStatus );
            }
        },
        error: function(errorXml, errorStatus, errorError){
            console.log( 'ErrorXml is: ' + errorXml );
            console.log( 'errorStatus is: ' + errorStatus );
            console.log( 'errorError is: ' + errorError );
        },
        complete: function(completeXml, completeStatus){
            console.log( 'completeXML is: ' + completeXml );
            console.log( 'completeStatus is: ' + completeStatus );
        }
    });
}

Of course the error: and complete: methods (is “methods” the right terminology?) are not necessary. Just something I was using to better understand the ajax function for jQuery.

So, I have three questions.

  1. Does the jQuery validate script have a default action that needs to be prevented? I know it looks like it doesn’t based on my code that is working, but I wanted to see if anyone had any additional insight into that.
  2. Why did I need to change the variable that was being serialize()‘d? What is different about when I use the jQuery .on("submit", function(e) { ... method of firing that allows $(this) to be serialize()‘d, vs when I fire the ajax submission via the submitHandler: method of the jQuery validate function?
  3. Is there a more efficient / better way I could be doing this?

Thank you so much for any and all feedback!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:21:40+00:00Added an answer on June 8, 2026 at 4:21 pm

    Does the jQuery validate script have a default action that needs to be prevented? I know it looks like it doesn’t based on my code that is working, but I wanted to see if anyone had any additional insight into that.

    No, it doesn’t. You can verify this by checking out the code that ultimately handle’s the submit event. You can also write a simple example to prove this:

    $("#myform").validate({
        submitHandler: function (form) {
            console.log('hi');
        }
    });
    

    The above example won’t actually post the form even when it’s valid. The plugin takes care of preventing the default action of the submit event if you specify a submitHandler.

    Why did I need to change the variable that was being serialize()’d?
    What is different about when I use the jQuery .on(“submit”,
    function(e) { … method of firing that allows $(this) to be
    serialize()’d, vs when I fire the ajax submission via the
    submitHandler: method of the jQuery validate function?

    Inside of submitHandler, this references the validator object that you’re applying to the form. That’s why $(this).serialize() doesn’t make sense like it did inside of an event handler (where this references the element the event occurred upon). Instead, you should jQuerify the form element that’s passed to the submitHandler:

    $("#myform").validate({
        submitHandler: function (form) {
            var serialized = $(form).serialize();
        }
    });
    

    Is there a more efficient / better way I could be doing this?

    Not really. I think this is a pretty standard, correct way to use submitHandler. If you want your AJAX submission code to be a bit shorter, you could look at the jQuery form plugin instead of doing a vanilla jQuery AJAX call (in fact, some of the example code in the docs for validate use that plugin).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Nick Craver really helped me out alot with this code in this thread jQuery
I've just started to get into using ViewModels. Can you guys check out this
Basically I am working on a review form in which users fill out items
This question most closely relates to the asp.net mvc3 framework. It started out as
Out of the blue today I started getting this error in Visual Studio 2008
I just started getting this error today, seemingly out of nowhere. Any one see
I just started out with jQuery and really only need it for some very
This started out as a general user question on Android forums. However it's become,
I just started out using IronRuby (but the behaviour seems consistent when I tested
This question started out with someone wanting to import every apparent 'line of text'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.