问题描述
我在 Ajax 调用中发送一些数据.其中一个值是设置为 FALSE 的布尔值.在 Ajax 调用的 PHP 脚本中,它总是被评估为 TRUE.有任何想法吗?
I'm sending some data in an Ajax call. One of the values is a boolean set to FALSE. It is always evaluated as TRUE in the PHP script called by the Ajax. Any ideas?
$.ajax({
type: "POST",
data: {photo_id: photo_id,
vote: 1,
undo_vote: false}, // This is the important boolean!
url: "../../build/ajaxes/vote.php",
success: function(data){
console.log(data);
}
});
在上面Ajax中调用的脚本vote.php中,我检查了布尔值:
In vote.php, the script that is called in the above Ajax, I check the boolean value:
if ($_POST['undo_vote'] == true) {
Photo::undo_vote($_POST['photo_id']);
} else {
Photo::vote($_POST['photo_id'], $_POST['vote']);
}
但总是满足 $_POST['undo_vote'] == true
条件.
推荐答案
帖子只是文本,而文本在 php 中将评估为 true.一个快速的解决方法是发送一个零而不是错误.你也可以在 PHP 中为你的 true 加上引号.
A post is just text, and text will evaluate as true in php. A quick fix would be to send a zero instead of false. You could also put quotes around your true in PHP.
if ($_POST['undo_vote'] == "true") {
Photo::undo_vote($_POST['photo_id']);
} else {
Photo::vote($_POST['photo_id'], $_POST['vote']);
}
然后你可以传入真/假文本.如果那是你喜欢的.
Then you can pass in true/false text. If that's what you prefer.
这篇关于在 jQuery ajax 数据中发送一个布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!