我正在使用此代码进行php MySql验证。
CheckID = document.getElementById('checkID_' + idCounter).value;
$.ajax({
type: "POST",
url: "checkval.php",
data: { check : CheckID }
}).done(function( data ) {
if(data !== "success");
{
var val = confirm(data);
if (val === false) {
return false;
}
}
});
此代码无法正常工作,之后脚本编写也无效。不知道检查错误。帮助我......
这是我的PHP脚本
<?php
$check = $_POST['check'];
$host = 'localhost';
$database = 'database';
$username = 'user';
$password = 'password';
$dbc = mysqli_connect($host,$username,$password,$database);
$counter = count(checkArray);
$checkno = $check;
$sql = "select claimno from check_details where checkno = $checkno";
$result = mysqli_query($dbc,$sql);
$rows = mysqli_num_rows($result);
if($rows != 0)
{
$claim = mysqli_fetch_array($result,MYSQLI_BOTH);
$claimno = $claim['claimno'];
$str = "Check Number:$checkno is already applied for $claimno.Are you sure to contniue ???";
echo $str;
}
else
{
echo "success";
}
?>
答案 0 :(得分:0)
您可以编写函数来返回“返回布尔状态值的承诺”:
function getMeStatus(checkID){
var def = $.Deferred(); // Create a deferred object
$.ajax({ // Make the ajax call
type: "POST",
url: "checkval.php",
data: {check: CheckID}
}).done(function (data) {
if (data !== "success"); {
var val = confirm(data);
if (val === false) {
// Status was false?
def.resolve(false);
}
else {
// Status was true?
def.resolve(true);
}
}
else {
// Status was true?
def.resolve(true);
}
}
// return a promise to supply the boolean status value later
return def.promise();
});
并使用它:
CheckID = document.getElementById('checkID_' + idCounter).value;
getMeStatus(checkID).done(function(status){
alert(status); // Use the value here
});