如果ajax页面的fetch数组结果包含任何内容,我想显示一条错误消息,如果数组结果为空则不执行任何操作。我的jQuery没有返回正确的值。请帮我解决这个问题。这是我的jQuery ajax代码。
function getmsg(obj){
//var invoc_num = $("#inv_num").val();
//alert(invoc_num);
$.ajax({
type: 'POST',
url: 'check_invoc_num.php',
data: "invoc_num="+ invoc_num,
async: false,
success: function(data) {
//alert(data);
var data = data.replace(/(^\s+|\s+$)/g,'');
alert(data);
if(data == "ok"){
}
else{
$('#inv_msg').html("invoice number exists..!");
}
}
});
}
我的PHP代码
if(isset($_SESSION['msg_agency_code'])){
$agency_code=$_SESSION['msg_agency_code'];
}
$invoc_num=$_POST['invoc_num'];
echo $CheckInvoicenum = $timesheetObj>CheckInvoicebyId($agency_code,$invoc_num);
if (empty($CheckInvoicenum)){
echo "ok";
} else {
echo "exists";
}
对于invoc_num的每个输入,我都得到了结果。
答案 0 :(得分:0)
Check this particular line from your php code
echo $CheckInvoicenum = $timesheetObj>CheckInvoicebyId($agency_code,$invoc_num);
It probably needs to be this.
$CheckInvoicenum = $timesheetObj->CheckInvoicebyId($agency_code,$invoc_num);
Also, your MySQL query is always going to return empty if no $agency_code
value is passed (or is always empty) and no records in the database match this. You may want to use the $agency_code
variable in the query, only if it is not empty.
You may also want to ensure that your database call returns the coreect value. To verify this, take care of the following -
a) Select only the columns that you need from the select query.
b) in the return statement doreturn $result[0];
if you need the first column's value