0

It is always submitting form; How I can return false using AJAX; I knew AJAX is asynchronous;

    if(theform.code.value!="")
    {

        $.ajax({
              type:'post',
              data:'action=codeverification&code='+theform.code.value,
              url:'jquery-ajax.php',
              success:function(html)
              {
                if(html=='false')
                {
                    alert('Please enter correct code');
                    theform.code.focus();
                    flag=false;
                }
              }
              })
    }
3
Contributors
7
Replies
27
Views
3 Years
Discussion Span
Last Post by Pravesh_1
0

Maybe there is a logic problem in your jQuery-Ajax.php page where it is returning false. Take a look to make sure..to test that force it to return true to see what happens.

0

It is returning false if condition fails; I tested it. Actually, line 10 is working fine but form is submitting on both case (true or false)..

0

I don't see anywhere in your sample code where you are trying to prevent the form from submitting. Just because the Ajax query returns the string false doesn't mean that is going to prevent a form submission. Is there code missing in your example?

0

Here is the code..

<script type="text/javascript">
function validate(theform)
{

    if(theform.code.value=="")
    {
        alert('Please Enter Verification Code');
        theform.code.focus();
        return false;
    }

    if(theform.code.value!="")
    {
        var flag=true;
        $.ajax({
              type:'post',
              data:'action=codeverification&code='+theform.code.value,
              url:'jquery-ajax.php',
              success:function(html)
              {
                if(html=='false')
                {
                    alert('Please enter correct code');
                    theform.code.focus();
                    flag=false;
                }
              }
              })
        alert(flag);
//      return flag;      
    }
</script>

<form method="post" onsubmit="return validate(this)"></form>
0

Ok, so after looking at your code, I beleive that what is happening is that the function is when executed assigns flag="true", but then finishes before the ajax callback is completed so you will never have the flag set to false before completing the function.

What you should do is have the button/trigger calling the AJAX validation, and the callback should submit the form instead of returning true/false.

For example...

$( "form" ).submit(function( event ) {
  event.preventDefault();
  // call your ajax function.
});

In your ajax function if successful:

$( "form" ).submit();
0

Still form is submitting; if condition fails..Below is the code

    <script type="text/javascript">
    $( "form" ).submit(function( event ) {
    event.preventDefault();
    // call your ajax function.
    });

    function validate(theform)
    {
    if(theform.code.value=="")
    {
    alert('Please Enter Verification Code');
    theform.code.focus();
    return false;
    }
    if(theform.code.value!="")
    {
    var flag=true;
    $.ajax({
    type:'post',
    data:'action=codeverification&code='+theform.code.value,
    url:'jquery-ajax.php',
    success:function(html)
    {
    if(html=='false')
    {
    alert('Please enter correct code');
    theform.code.focus();
    flag=false;
    }
    }
    })
    alert(flag);
    // return flag;
    }
    </script>
    <form method="post" onsubmit="return validate(this)"></form>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.