简体   繁体   English

从ajax返回值,回到它的JS函数

[英]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() . 我有HTML Form ,并且有一个带有onclick=validationFunction()的按钮submit 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. 现在,在此函数中,如果enter code here正确或错误,则enter code here表单的值。 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. 此外,它具有1个input Field ,必须检查该input Field以进行验证,并且还再次从数据库中检查该字段是否存在值。 This part is done via ajax . 这部分是通过ajax完成的。 Below the ajax call, there is a return value (boolen) for the function validationFucntion() . 在ajax调用下方,有一个函数validationFucntion()的返回值(boolen)validationFucntion()

Now, what i want. 现在,我想要什么。 i want either of the two things. 我想要两件事之一。

1) ajax should return true or false within its success 1)ajax在success内应返回true或false

2) or ajax should send the value just below where the ajax call ends. 2)或ajax应该在ajax调用结束处的下面发送该值。 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 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 你可以在这里找到插件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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