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;
                }
              }
              })
    }

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.

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)..

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?

do you have a return in your submit button

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>

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();

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>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.