发送jQuery中的ajax数据的布尔值 [英] Send a boolean value in jQuery ajax data

查看:2109
本文介绍了发送jQuery中的ajax数据的布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在发送Ajax调用的一些数据。一个值是一个布尔值设置为FALSE。它总是值为TRUE在调用Ajax的PHP脚本。有任何想法吗?

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

在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'] ==真条件始终得到满足。

But the $_POST['undo_vote'] == true condition is ALWAYS met.

推荐答案

一个帖子是只是文本和文本将评估作为真正在PHP。速战速决会给人以虚假零代替。你也可以把周围的真实报价在PHP。

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

然后你可以通过在真/假的文本。如果这就是你preFER。

Then you can pass in true/false text. If that's what you prefer.

这篇关于发送jQuery中的ajax数据的布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆