简体   繁体   中英

returning value from ajax, back to the JS function that it

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 che enter code here cked if enter 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, im 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);
?>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM