I know the title is very annoying but anyways I start my question. I have created a function in a class that's kinda a multi-purpose one. I gave it a parameter which is of boolean type and of course only accepts "true" and "false" as values. Next, I am calling out that function through another PHP file on which the data is posted through ajax and that ajax is called through a button on my HTML page. Let me demonstrate it as a diagram:
I hope you've understood my program flow, now let's come to the code or in other words here is what I've tried:
HTML Page's button:
document.write('<span class="pull-right" style="margin-top: -30px; margin-right: 20px;"><button id="accept_offer" class="btn btn-success" onclick="setPostOfferAcceptance(32);">Accept Offer</button></span>');
AJAX Page:
<?php
require_once '../../Classes/MyClass.php';
$offer_id = $_POST["id"];
$acceptance = $_POST["acceptance"];
$accepted = MyClass::SetOfferAcceptance($offer_id, $acceptance);
if($accepted) $data["status"] = "success";
else $data["status"] = "error";
echo json_encode($data);
?>
MyClass's Function:
public static function SetOfferAcceptance($offerId, $acceptance) {
if(Utilities::IsValid($offerId) && Utilities::IsValid($acceptance)) {
Database::OpenConnection();
$offerId = Utilities::SafeString(Database::$databaseConnection, $offerId);
$acceptance = Utilities::SafeString(Database::$databaseConnection, $acceptance);
$query = "";
if($acceptance == true) {
$query = Database::$databaseConnection->prepare("UPDATE post_offer SET Accepted=1 WHERE Offer_ID = ?");
} else {
$query = Database::$databaseConnection->prepare("UPDATE post_offer SET Accepted=0 WHERE Offer_ID = ?");
}
$query->bind_param("i", $offerId);
$result = $query->execute();
Database::CloseConnection();
if($result) return 1; else return -1;
}
}
And sorry, but finally, here's my javascript function that posts the data to the AJAX page:
function setPostOfferAcceptance(offer_id) {
if($("#accept_offer").text() == "Accept Offer") {
$.post("admin/post_offer/set_post_offer_acceptance.php", {id: offer_id, acceptance: true}, function(data){
if(data.status == "success") {
$("#acceptedOfferModal").modal("show");
setTimeout(function(){ $("#acceptedOfferModal").modal("hide"); }, 2500);
$("#accept_offer").text("Unaccept Offer");
console.log(data.status);
}
}, 'json');
} else if($("#accept_offer").text() == "Unaccept Offer") {
$.post("admin/post_offer/set_post_offer_acceptance.php", {id: offer_id, acceptance: false}, function(data){
if(data.status == "success") {
$("#unacceptedOfferModal").modal("show");
setTimeout(function(){ $("#unacceptedOfferModal").modal("hide"); }, 2500);
$("#accept_offer").text("Accept Offer");
console.log(data.status);
}
}, 'json');
}
}
EDIT: I forgot to post the JS code. But I've updated it now. Please consider it. Thanks.
The problem is that the function SetOfferAcceptance()
is never running the else condition which is that if $acceptance
is not equal to true
.
Why? Can anyone please point out my mistake.
Thanks.