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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:29:08+00:00 2026-06-12T05:29:08+00:00

i am trying to validate a form before adding the visitor info to a

  • 0

i am trying to validate a form before adding the visitor info to a mysql db. The jQuery script follows:

var phone = $("input#phone").val();
    if (phone == "") {
  $("label#phone_error").show();
  $("input#phone").focus();
  return false;
}

//Captcha validation
var security_code = $("input#security_code").val();
$.ajaxSetup({cache: false})
$.get('getsession.php', {requested: 'seccode'}, function (data) {
var session = data;

if (security_code !== session) {
    $("label#security_code_error").show();
    $("input#security_code").focus();
    alert ('NO MATCH: '+security_code+' '+session);
    return false; // <= FUNCTION SHOULD EXIT HERE
} else {
    alert ('MATCH!!!: '+security_code+' '+session);
}
});     

    var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone;
    //alert (dataString);return false;

    $.ajax({
  type: "POST",
  url: "bin/process.php",
  data: dataString,
  success: function() {
    $('#contact_form').html("<div id='message'></div>");
    $('#message').html("<h2>Contact Form Submitted!</h2>")
    .append("<p>We will be in touch soon.</p>")
    .hide()
    .fadeIn(1500, function() {
      $('#message').append("<img id='checkmark' src='images/check.png' />");
    });
  }
 });
return false;
});
});

If a name is missing, then the form displays an error and focus the field to be completed. The problem is the captcha validation. If it matches, the form is successfully processed and the lead gets added to the DB, however, if the security code entered in a text filed does not match the generated code, the error is displayed but the form info is still added to the DB. Looks like the “return false; // <= FUNCTION SHOULD EXIT HERE” is not recognized.
Any suggestion on what is wrong with my code?

  • 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-12T05:29:10+00:00Added an answer on June 12, 2026 at 5:29 am

    The return false that you expect to end your execution is in the callback function for an AJAX request. The first A in AJAX stands for asynchronous, so that function isn’t executed until the request returns a successful response.

    However, since it is asynchronous, the rest of the function making that AJAX request will still execute, therefore submitting a separate AJAX POST request to submit the form data, completely unrelated to the first AJAX call.

    What you need to do is move the below part of your code into the callback function for the AJAX request that checks the captcha, and only execute it if the validation was successful.

    var dataString = 'name=' + name + '&email=' + email + '&phone=' + phone;
    //alert (dataString);return false;
    $.ajax({
        type: "POST",
        url: "bin/process.php",
        data: dataString,
        success: function() {
            $('#contact_form').html("<div id='message'></div>");
            $('#message').html("<h2>Contact Form Submitted!</h2>").append("<p>We will be in touch soon.</p>").hide().fadeIn(1500, function() {
                $('#message').append("<img id='checkmark' src='images/check.png' />");
            });
        }
    });
    

    The entire code would look like this:

    var phone = $("input#phone").val();
    if (phone == "") {
        $("label#phone_error").show();
        $("input#phone").focus();
        return false;
    }
    
    //Captcha validation
    var security_code = $("input#security_code").val();
    $.ajaxSetup({
        cache: false
    })
    $.get('getsession.php', {
        requested: 'seccode'
    }, function(data) {
        var session = data;
    
        if (security_code !== session) {
            $("label#security_code_error").show();
            $("input#security_code").focus();
            alert('NO MATCH: ' + security_code + ' ' + session);
            return false; // <= FUNCTION SHOULD EXIT HERE
        } else {
            alert('MATCH!!!: ' + security_code + ' ' + session);
            // successful, so let's submit the details
            var dataString = 'name=' + name + '&email=' + email + '&phone=' + phone;
            $.ajax({
                type: "POST",
                url: "bin/process.php",
                data: dataString,
                success: function() {
                    $('#contact_form').html("<div id='message'></div>");
                    $('#message').html("<h2>Contact Form Submitted!</h2>").append("<p>We will be in touch soon.</p>").hide().fadeIn(1500, function() {
                        $('#message').append("<img id='checkmark' src='images/check.png' />");
                    });
                }
            });
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to validate some form data before adding it to a database however
I'm trying to validate my inputs with jQuery before a form is sent. This
I am trying to validate user input from a form before submit but it
I am trying to validate a form with the jquery-validate plugin. The html page
I have not really used JavaScript before but I am trying to validate form
I'm trying to validate a form by checking the required fields with a jQuery
I am trying to validate this form before it is submitted to the order
I have a form within a modal that i am trying to validate before
I have been trying to make this form validate before sending an email to
I'm trying to Validate my form before it's being sent to the server. I

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.