2017-07-17 92 views
1

我有這個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"; 
} 

腳本工作正常,所以如果我有exceededempty,什麼都不會被添加或在數據庫中更改。

我的問題是在這裏:

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); 
} 

如果,如果我有在控制檯exceededempty,我會得到q_added響應的警報。

我試着將以下從errorsuccess功能,但仍然是相同的:

if(response == "empty") 
{ 
    alert("Not enough quantity in the storage!"); 
} 
if(response == "exceeded") 
{ 
    alert("Quantity given is more than the exist quantity in stock"); 
} 

我甚至試過用ifelse ifelse還是一樣。

編輯

仍然有,即使我改劇本同樣的問題:

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); 
}, 
+1

這意味着「空」和「超出」不是錯誤,而是成功消息。 –

+0

如果你想錯誤地得到'empty'或'exceeded',那麼在服務器端設置響應代碼就像500,402等...... –

+0

我已經將它們添加到成功函數中,並且仍然是相同的錯誤 – droidnation

回答

1

你的腳本是正確的,但你檢查這是不對的錯誤部分的響應。使用這個:

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出現錯誤。

+0

如果您能看到我的問題的結尾,我已經做了更改。但仍然不起作用 – droidnation

+0

@droidnation在成功函數中檢查您在響應中獲得的內容。 –

+0

我安慰它,我可以在控制檯看到「超過」或「空」# – droidnation