我知道标题非常烦人,但无论如何我开始提问。我在类中创建了一个多功能的函数。我给它一个布尔类型的参数,当然只接受“true”和“false”作为值。接下来,我通过另一个PHP文件调用该函数,在该文件上通过ajax发布数据,并通过HTML页面上的按钮调用ajax。让我以图表的形式展示它:
我希望你已经了解我的程序流程,现在让我们来看看代码,换句话说,这就是我尝试过的:
HTML网页按钮:
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页面
<?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的功能:
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;
}
}
抱歉,最后,这是我的javascript函数,它将数据发布到AJAX页面:
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');
}
}
编辑:我忘记发布JS代码了。但我现在已经更新了。请考虑一下。感谢。
问题是函数SetOfferAcceptance()
永远不会运行else条件,即$acceptance
不等于true
。
为什么呢?任何人都可以指出我的错误。
感谢。
答案 0 :(得分:0)
如果acceptance
是布尔值,则使用以下if...else
,即tripple等于$acceptance === true
:
// In AJAX page
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 = ?");
}
请参考:“How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?”
<强>更新强>
检查$acceptance
中的SetOfferAcceptace
值,或将boolean
值传递给SetOfferAcceptance
。后者的例子:
$offer_id = $_POST["id"];
$acceptance = false;
if (isset($_POST["acceptance"]) && $_POST["acceptance"] == 'accepted') {
$acceptance = true;
}
$accepted = MyClass::SetOfferAcceptance($offer_id, $acceptance);
答案 1 :(得分:0)
谢谢大家的答案,但我想出了另一种选择。这不是使$acceptance
成为布尔值而是使其成为字符串并将我的JS代码转换为:
function setPostOfferAcceptance(offer_id) {
if($("#accept_offer").text() == "Accept Offer") {
$.post("admin/post_offer/set_post_offer_acceptance.php", {id: offer_id, acceptance: "accepted"}, 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: "unaccepted"}, 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');
}
}
我班的功能是:
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 == "accepted") {
$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;
}
}
这很有效! :)
再次感谢 @vee 。
答案 2 :(得分:0)
如果我没有弄错,你的问题源于自动类型转换。
if($result) return 1; else return -1; // both of these evaluate to boolean true because -1 is an integer
if($result) return 1; else return 0; // evaluates to true/false respectively
if($result) return true; else return false; // evaluates to true/false respectively
最后一行是您应该诚实使用的。