PHP AJAX返回值正常工作,但与错误警报消息
我有这个PHP脚本在刀刃特定药物的药丸的数量,即正常工作:PHP AJAX返回值正常工作,但与错误警报消息
$clinic_id = $_SESSION['clinic_id'];
$visit_id = $_POST['visit_id'];
$patient_id = $_POST['patient_id'];
$nid = $_POST['nid'];
$did = $_POST['did'];
$cid = $_POST['cid'];
$diagnosis = $_POST['diagnosis'];
$medication_id = $_POST['medication_id'];
$medication_quantity = $_POST['medication_quantity'];
$consultation_result = $_POST['consultation_result'];
$ensureQuantity = "
SELECT t1.med_pharmacy_id
, t1.med_id
, sum(t2.given_quantity) as given_pills
, t1.med_tablet - ((sum(t2.given_quantity)*t1.med_tablet)/t1.med_pill) as still_tablets
, (t1.med_pill-sum(t2.given_quantity)) as still_pills
FROM med_pharmacy t1
, consultation_med t2
, medication t3
WHERE t1.med_pharmacy_id = t2.med_pharmacy_id
AND t1.med_id = t3.med_id
AND t1.clinic_id = :cid
AND t1.med_pharmacy_id = :mid
GROUP
BY t1.med_pharmacy_id
, t1.med_id
, t3.med_name
, t1.med_expiry
, t1.med_barcode
, t1.med_tablet
, t1.med_pill
, t1.med_received
";
$execEnsureQuantity = $conn->prepare($ensureQuantity);
$execEnsureQuantity->bindValue(':cid', $clinic_id);
$execEnsureQuantity->bindValue(':mid', $medication_id);
$execEnsureQuantity->execute();
$res = $execEnsureQuantity->fetch();
然后,它会进行比较从一个文本框的附加值由用户:
if($res['still_pills']==0)
{
echo "empty";
}
else if($medication_quantity > $res['still_pills'])
{
echo "exceeded";
}
所以,如果没有更多的药,我需要呼应empty
,如果用户输入一个数比越大,存在的一个,它会响应“超标“,那么如果数字是合乎逻辑的:
else
{
$addConsultation = "INSERT INTO consultation(nurse_list_id, doctor_list_id,
complication_name, diagnosis_id, visit_id, consultation_result, clinic_id,
patient_id)
VALUES(:nid, :did, :cid, :diagnosis, :visit_id, :consultation_result,
:clinic_id, :patient_id)";
$execAddConsultation = $conn->prepare($addConsultation);
$execAddConsultation->bindValue(":nid", $nid);
$execAddConsultation->bindValue(":did", $did);
$execAddConsultation->bindValue(":cid", $cid);
$execAddConsultation->bindValue(":diagnosis", $diagnosis);
$execAddConsultation->bindValue(":visit_id", $visit_id);
$execAddConsultation->bindValue(":consultation_result", $consultation_result);
$execAddConsultation->bindValue(":clinic_id", $clinic_id);
$execAddConsultation->bindValue(":patient_id", $patient_id);
$execAddConsultation->execute();
$lastConsultId = $conn->lastInsertId();
$insertQuantity = "INSERT INTO consultation_med(consultation_id, med_pharmacy_id, given_quantity, date_given, clinic_id)
VALUES(:consult_id, :mp_id, :gq, :dg, :cid)";
$execInsertQuantity = $conn->prepare($insertQuantity);
$execInsertQuantity->bindValue(':consult_id', $lastConsultId);
$execInsertQuantity->bindValue(':mp_id', $res['med_pharmacy_id']);
$execInsertQuantity->bindValue(':gq', $medication_quantity);
$execInsertQuantity->bindValue(':dg', date('Y-m-d H:i:s'));
$execInsertQuantity->bindValue(':cid', $clinic_id);
$execInsertQuantity->execute();
echo "q_added";
}
脚本工作正常,所以如果我有exceeded
或empty
,什么都不会被添加或在数据库中更改。
我的问题是在这里:
success:function(response)
{
if(response == "q_added")
{
alert("Data Added. Please add more, or close the box!");
$("#doctor_list_id").val("select");
$("#nurse_list_id").val("select");
$("#complication_name_2").val("select");
$("#diagnosis").val("select");
$("#medication_quantity").val("");
$("#medication_id").val("select");
$("#consultation_result").val("");
$("#complication_name_3").val("");
//console.log(response);
}
},
error:function(response)
{
if(response == "empty")
{
alert("Not enough quantity in the storage!");
}
if(response == "exceeded")
{
alert("Quantity given is more than the exist quantity in stock");
}
console.log(response);
}
如果,如果我有在控制台exceeded
或empty
,我会得到q_added
响应的警报。
我试着将以下从error
到success
功能,但仍然是相同的:
if(response == "empty")
{
alert("Not enough quantity in the storage!");
}
if(response == "exceeded")
{
alert("Quantity given is more than the exist quantity in stock");
}
我甚至试过用if
else if
else
还是一样。
编辑
仍然有,即使我改剧本同样的问题:
success:function(response)
{
if(response == "q_added")
{
alert("Data Added. Please add more, or close the box!");
$("#doctor_list_id").val("select");
$("#nurse_list_id").val("select");
$("#complication_name_2").val("select");
$("#diagnosis").val("select");
$("#medication_quantity").val("");
$("#medication_id").val("select");
$("#consultation_result").val("");
$("#complication_name_3").val("");
//console.log(response);
}
if(response == "empty")
{
alert("Not enough quantity in the storage!");
}
if(response == "exceeded")
{
alert("Quantity given is more than the exist quantity in stock");
}
console.log(response);
},
你的脚本是正确的,但你检查这是不对的错误部分的响应。使用这个:
success:function(response)
{
if($.trim(response) == "empty")
{
alert("Not enough quantity in the storage!");
}
else if($.trim(response) == "exceeded")
{
alert("Quantity given is more than the exist quantity in stock");
}
else if(response == "q_added")
{
alert("Data Added. Please add more, or close the box!");
$("#doctor_list_id").val("select");
$("#nurse_list_id").val("select");
$("#complication_name_2").val("select");
$("#diagnosis").val("select");
$("#medication_quantity").val("");
$("#medication_id").val("select");
$("#consultation_result").val("");
$("#complication_name_3").val("");
//console.log(response);
}
console.log(response);
},
error:function(xhr, status, error)
{
}
ajax中的错误函数不是你从php中感受到的错误。
当你的脚本不工作或者你的ajax因任何原因失败时,ajax出现错误。
如果您能看到我的问题的结尾,我已经做了更改。但仍然不起作用 – droidnation
@droidnation在成功函数中检查您在响应中获得的内容。 –
我安慰它,我可以在控制台看到“超过”或“空”# – droidnation
这意味着“空”和“超出”不是错误,而是成功消息。 –
如果你想错误地得到'empty'或'exceeded',那么在服务器端设置响应代码就像500,402等...... –
我已经将它们添加到成功函数中,并且仍然是相同的错误 – droidnation