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 6889575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:10:25+00:00 2026-05-27T06:10:25+00:00

I have the following JQuery to submit a contact form with jquery ajax. This

  • 0

I have the following JQuery to submit a contact form with jquery ajax. This is the first time I attempted to use ajax and I don’t seem to be able to make it work.

When I submit the form, I get a full page refresh and then taken to the forms action page which is a php file that processes the form. I can’t figure out why. Can you help?

The php file processes the info properly because he message I get at the end is correct — it just gives it to me on the php page.

Thank you!

$(document).ready(function() {

    ...

    // -----------------------------------------------
    // EMAIL FORM SUBMIT CLICK
    // -----------------------------------------------
    $("#contact_form #btnSubmit").click(function() {

        // assign form elements to variables
        var txbName = $("input[name=txbName]");
        var txbEmail = $("input[name=txbEmail]");
        var txbMessage = $("textarea[name=txbMessage]");
        var txbRecaptcha = $("input[id=recaptcha_response_field]");

        // assign form element values
        var txbNameVal = txbName.val();
        var txbEmailVal = txbEmail.val();
        var txbMessageVal = txbMessage.val();
        var txbRecaptchaVal = txbRecaptcha.val();

        // validate NAME field
        if($.trim(txbNameVal) == "" || $.trim(txbNameVal.length) < 2){
            $(txbName).focus().fadeOut("slow").fadeIn("slow").fadeOut("slow").fadeIn("slow");
            response('- Name is required!','show');
            return false;
        } else {
            response('','hide');
        }

        // validate EMAIL field
        if($.trim(txbEmailVal) == "" || $.trim(txbEmailVal.length) < 6){
            $(txbEmail).focus().fadeOut("slow").fadeIn("slow").fadeOut("slow").fadeIn("slow");
            response('- Email is required!','show');
            return false;
        } else {
            response('','hide');
        }

        // validate EMAIL format
        if(!txbEmailVal.match(/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/)){
            $(txbEmail).focus().fadeOut("slow").fadeIn("slow").fadeOut("slow").fadeIn("slow");
            response('- Email format is incorrect!','show');
            return false;
        } else {
            response('','hide');
        }

        // validate MESSAGE field
        if($.trim(txbMessageVal) == "" || $.trim(txbMessageVal.length) < 10){
            response('- Message is required!','show');
            $(txbMessage).focus().fadeOut("slow").fadeIn("slow").fadeOut("slow").fadeIn("slow");
            return false;
        } else {
            response('','hide');
        }

        // validate reCAPTCHA field
        if($.trim(txbRecaptchaVal) == ""){
            response('- Recaptcha is required!','show');
            $(txbRecaptcha).focus().fadeOut("slow").fadeIn("slow").fadeOut("slow").fadeIn("slow");
            return false;
        } else {
            response('','hide');
        }

        /*
        if we've passed all of the above validation,
        then show the ajax loading icon, serialize
        the form data and call the ajax submit function
        */
        $('.loading').show();
        var formData = $('form').serialize();
        submitForm(formData);
    });
});

// -----------------------------------------------
// AJAX FORM SUBMIT - OUTSIDE OF DOCUMENT READY!
// -----------------------------------------------
function submitForm(formData){
    $.ajax({    
        type: 'POST',
        url: 'contact.php',     
        data: formData,
        dataType: 'json',
        cache: false,
        timeout: 7000,
        success: function(data) {           
            // test
            alert(data.msg);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // test
            alert(errorThrown + textStatus);            
        },              
        complete: function(XMLHttpRequest, status) {            
            $('form')[0].reset();
        }
    }); 
}
  • 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-05-27T06:10:25+00:00Added an answer on May 27, 2026 at 6:10 am

    You need to return false after calling submitForm to prevent the browser from performing the default action (submitting the page with a refresh).

    By the way, rather than this:

    $("#contact_form #btnSubmit").click(function() {
        // ...
    });
    

    I would use this:

    $("#contact_form").submit(function() {
        // ...
    });
    

    Then if the user presses enter in a field or otherwise submits the form without clicking the button, your code will handle that, too.

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

Sidebar

Related Questions

I have the following jQuery: $(form).submit(function() { //DO STUFF HERE }); But, it's not
I have the following jQuery code: $('.save').submit(function(e){ var formElement = $(this); var data =
I have the following HTML <select onchange=this.form.submit() name=name> <option value=9995101E01#17201044055PM>9995101E01#17201044055PM</option> <option value=GRR-Example>GRR-Example</option> <option value=admin>admin</option>
i have the following javascript file named coupon.js - jQuery(document).ready(function(){ jQuery('.appnitro').submit( function() { $.ajax({
I have the following jquery script: $(function(){ $('#top-menu').on('click', 'a.change-menu', function(e){ e.preventDefault() $(#menu-change-div).load($(this).attr(href)); }); });
I have the following jQuery $.ajax({ type: GET, url: http://f.cl.ly/items/0i1V1L1k2F440L1m2Y0G/pointdata.xml, dataType: xml, success: parseXml
I have this following jquery text fly-in animation.Here is my code before I explain
I have this MVC form with 2 dropdownlist where selection on the first dropdown
I have successfully used ajax to refresh in-line, via ajax, a contact form. I'm
I have the following jQuery code: $('#picture').live('change', function() { $(#formupload).ajaxForm({ target: '#newpic' }).submit(); });

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.