Reputation: 4972
I have this PHP script where it counts the number of pills of a specific medication, that is working properly:
$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();
Then, it will compare it to the added value by a user from a text box:
if($res['still_pills']==0)
{
echo "empty";
}
else if($medication_quantity > $res['still_pills'])
{
echo "exceeded";
}
So if there is no more pills, I need to echo empty
and if the user typed a number bigger than the exist one, it will echo "exceeded", then if number is logical:
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";
}
The script is working fine, so if I had exceeded
or empty
, nothing will be added or changed at the database.
My problem is in here:
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);
}
if if I have at the console exceeded
or empty
, I will get the alert of q_added
response.
I tried to move the following from error
to success
function but still the same:
if(response == "empty")
{
alert("Not enough quantity in the storage!");
}
if(response == "exceeded")
{
alert("Quantity given is more than the exist quantity in stock");
}
I even tried to use if
else if
else
and still the same.
EDIT
Still having the same problem even when I changed the script to:
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);
},
Upvotes: 1
Views: 65
Reputation: 15603
Your script is correct but you are check the response in the error part which is wrong. use this:
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)
{
}
The error function in ajax is not the error that you are perception from php.
Error in ajax comes when your script is not working or your ajax is failing due to any reason.
Upvotes: 1