[英]returning value from ajax, back to the JS function that it
這是問題所在。
我有HTML Form
,並且有一個帶有onclick=validationFunction()
的按鈕submit
。 當我單擊此按鈕時,表單中的值將轉到此功能。
現在,在此函數中,如果enter code here
正確或錯誤,則enter code here
表單的值。 此外,它具有1個input Field
,必須檢查該input Field
以進行驗證,並且還再次從數據庫中檢查該字段是否存在值。 這部分是通過ajax
完成的。 在ajax調用下方,有一個函數validationFucntion()
的返回值(boolen)
值validationFucntion()
。
現在,我想要什么。 我想要兩件事之一。
1)ajax在success
內應返回true或false
2)或ajax應該在ajax調用結束處的下面發送該值。 到現在為止,即時通訊無法成功完成這兩項工作。
這是示例偽代碼。
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);
?>
試試這個插件
<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>
並將其保留在您的表單中
<form id="tempForm" enctype="multipart/form-data">
<input type="file" name="" id="" />
</form>
你可以在這里找到插件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.