1

I'm validating a form using Javascript and jQuery.

At the end of the validation function I just AJAX to check an entry against a SQL database to see if it already exists. I have that working fine but I don't seem to be able to update the Javascript variable to indicate whether the check returned that the entry exists or not. Below is a pared down version of the validation function.

At the end when I do the AJAX call if the customer exists, I try to set the isformcomplete variable to false but that doesn't work, it's almost as if it's not reading that line and without that set properly, the form gets erroneously submitted.

What am I missing here? The AJAX call is working properly. Thanks.

<form method="post" action="../../../scripts/create-customer.asp" id="create-customer" onsubmit="return create_customer();">

function create_customer()
{
    var isformcomplete = true;
    message = "The following tasks need to be completed before continuing:\n\n";
    if ( $("#customerno").val() == "" )
    {
        message += "* Select the customer's country and enter its unique identifier\n";
        isformcomplete = false;
    }
    if ( $("#product").val() == "" )
    {
        message += "* Select a product\n";
        isformcomplete = false;
    }

    if (isformcomplete == false)
        { alert(message); }
    else
    {
        $("#alertsDynamic").slideUp();
        $.post("/global-marketing/scripts/check-new-customer.asp",{ fromsubmit: true, customer: $("#customerno").val()+"|"+$("#product").val()}, 
        function(data, status)
            {
                if ( data == "true" )
                {
                    $("#error-message").html($("#customerno").val()+" "+$("#product").val()+" already exists");
                    $("#alertsDynamic").slideDown();
                    isformcomplete = false;
                }
            }
        );
    }
    return isformcomplete;
5
  • 2
    Can try one of this if (data == true) or if (data) in place of if ( data == "true" ) Commented Jul 26, 2017 at 13:36
  • 1
    The ajax call is async. Execution has probably reached the end of the function before you receive the reply from the ajax request. Commented Jul 26, 2017 at 13:41
  • @jingesh That's not the issue, it gets to the block of code after that, it's just not setting the isformcomplete variable Commented Jul 26, 2017 at 13:42
  • @astrocrack Ah I see. Doesn't seem to be a way to get this to work for this purpose. Commented Jul 26, 2017 at 13:54
  • 1
    Yes you should always be careful with async requests The top answer seems to deal with that. Commented Jul 26, 2017 at 14:11

1 Answer 1

3

you should always return false in your function

function create_customer()
{
  message = "The following tasks need to be completed before continuing:\n\n";
  if ( $("#customerno").val() == "" )
  {
    message += "* Select the customer's country and enter its unique 
               identifier\n";
    isformcomplete = false;
}
if ( $("#product").val() == "" )
{
    message += "* Select a product\n";
    isformcomplete = false;
}

if (isformcomplete == false)
    { alert(message); }
else
{
    $("#alertsDynamic").slideUp();
    $.post("/global-marketing/scripts/check-new-customer.asp",{ fromsubmit: true, customer: $("#customerno").val()+"|"+$("#product").val()}, 
    function(data, status)
        {
            if ( data == "true" )
            {
                $("#error-message").html($("#customerno").val()+" "+$("#product").val()+" already exists");
                $("#alertsDynamic").slideDown();
                // Here you need to send the form in case every thing is fine

            }else{
                $("form").submit();
            }
        }
    );
}
return false;
Sign up to request clarification or add additional context in comments.

4 Comments

If data=="true" then I need to not submit the form
see the updated answer you can submit form in else case of your condition
Yeah, that doesn't work either. I'm guessing the .submit triggers the onsubmit call on the form again. Nothing happens.
Ok, with some tweaking I got it to work. I moved it out from the onsubmit and changed the submit button to an href linked button that calls the validation code and submits it if it passes, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.