0

here is the problem.

i have HTML Form and it has a button submit with an onclick=validationFunction(). When i click this button, values from form goes to this function.

Now, in this function, the values of the form are cheenter code herecked ifenter code here they are correct or not. In addition, it has 1 input Field who has to be checked for validation, and also checked again from database to see it that value exists there. This part is done via ajax. Below the ajax call, there is a return value(boolen) for the function validationFucntion().

Now, what i want. i want either of the two things.

1) ajax should return true or false within its success

2) or ajax should send the value just below where the ajax call ends. By now, i m failing big times to do either of the things.

Here is a sample pseudo code.

    function validationFunction()
{

     validations checks in progress
     $.ajax({
     url:'checkIfNumberExists.php',
     data : {
             'number : num //this num is coming from above
            },

     method:'GET',
     success: function(data)
            {
                console.log("Return Value = "+this.toReturn);
                if(  (this.toReturn) > 0 )
                {
                     either return validationFunction from here or set a flag.
                }
                else
                {
                     either return validationFunction from here or set a flag.
                }

     });
}

checkIfNumberExists.php

<?php

$num = $_GET['number'];
$toReturn = 0 ;

$queryCheckNo = mysql_query('SELECT * FROM `TABLE` WHERE `number_from_table`="'.$num.'" ');


while($row = mysql_fetch_assoc($queryCheckNo)){
    $toReturn++;
}
echo ($toReturn);
?>
irshad.ahmad
  • 276
  • 3
  • 9
  • 24
  • $.ajax is asynchronous, and function call is synchronous – salexch Jan 20 '13 at 20:40
  • http://stackoverflow.com/questions/5316697/jquery-return-data-after-ajax-call-success – salexch Jan 20 '13 at 20:40
  • tried these links, but still the problem remains the same, maybe i have to change the design. – irshad.ahmad Jan 20 '13 at 21:32
  • to get what you want , you can't use: onsubmit="return validationFunction()" cause your function is asynchronous. you need to design your own bloking/unblocking ui for the form while the ajax processes.. – salexch Jan 21 '13 at 08:08

1 Answers1

0

try this plug in

<script>
    // wait for the DOM to be loaded
    $(document).ready(function() 
    {
        // bind 'myForm' and provide a simple callback function
       $("#tempForm").ajaxForm({
       url:'../calling action or servlet',
       type:'post',
       beforeSend:function()
       {
         alert("perform action before making the ajax call like showing spinner image");
       },
       success:function(e){
        alert("data is"+e);
            alert("now do whatever you want with the data");
       }
       });
    });
</script>

and keep this inside your form

<form id="tempForm" enctype="multipart/form-data">
<input type="file" name="" id="" />
</form>

and you can find the plug in here