php 通过 AJAX 发布的布尔变量在服务器端被视为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3654454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:34:40  来源:igfitidea点击:

boolean variables posted through AJAX being treated as strings in server side

phpjqueryajaxbooleanpost-processing

提问by Sandeepan Nath

Following is a part of an AJAX functionality to add classes and packs to session cart:-

以下是将类和包添加到会话购物车的 AJAX 功能的一部分:-

The jquery part

jquery部分

function addClassToCart(itemId)
{
   addItemToCart(itemId,true);
}

function addPackToCart(itemId)
{
   addItemToCart(itemId,false);
}

function addItemToCart(itemId,isClass)
{   
     $.post(url+"/ajax/add_cart", { operation: 'add_cart','isClass':isClass, 'itemId': itemId},
        function(data)
        {
               if(data.success)
               {
                      alert("item added to cart");
               }
        }, "json");

}

The AJAX request processing php part -

AJAX请求处理php部分——

//Checking operation and other posted parameters
if($_POST['isClass'])
{
  //Code to add class to session cart

}
else
{
  //Code to add pack to session cart
}

The strange thing

奇怪的事

No matter whether I pass true/false (by calling addClassToCart() and addPackToCart()), always the code to add class to session cart executes.
If I put echo statements there like this:-

无论我是否通过 true/false(通过调用 addClassToCart() 和 addPackToCart()),将类添加到会话购物车的代码总是会执行。
如果我像这样把 echo 语句放在那里:-

    if($_POST['isClass'])
    {
      echo "see if condition ".$_POST['isClass'];
    }
    else
    {
      echo "see else condition ".$_POST['isClass'];
    }

This is the output:-

这是输出:-

addClassToCart() see if condition true
addPackToCart() see if condition false

addClassToCart() see if condition true
addPackToCart() see if condition false

Putting conditions like this in the jquery code however works fine:-

将这样的条件放在 jquery 代码中,但效果很好:-

function addItemToCart(itemId,isClass)
 {  
     if(isClass)
        alert("is class");
     else
        alert("is pack");
 }

Finally, if I alter the server side code to this:-

最后,如果我将服务器端代码更改为:-

if($_POST['isClass'] === true)
        {
          echo "see if condition ".$_POST['isClass'];
        }
        else
        {
          echo "see else condition ".$_POST['isClass'];
        }

These are the outputs -

这些是输出 -

addClassToCart() see else condition true
addPackToCart() see else condition false

addClassToCart() see else condition true
addPackToCart() see else condition false

So, why is the boolean variable treated as a string here? Am I doing something wrong in posting parameters?

那么,为什么布尔变量在这里被视为字符串呢?我在发布参数时做错了什么吗?

Thanks, Sandeepan

谢谢,桑迪潘

回答by Tamara

Also you can use filter_var function with filter FILTER_VALIDATE_BOOLEAN. According to php documentation it

您也可以将 filter_var 函数与过滤器 FILTER_VALIDATE_BOOLEAN 一起使用。根据php文档它

Returns TRUE for "1", "true", "on" and "yes". Returns FALSE otherwise. If FILTER_NULL_ON_FAILURE is set, FALSE is returned only for "0", "false", "off", "no", and "", and NULL is returned for all non-boolean values.

为“1”、“true”、“on”和“yes”返回 TRUE。否则返回 FALSE。如果设置了 FILTER_NULL_ON_FAILURE,则仅对“0”、“false”、“off”、“no”和“”返回 FALSE,对所有非布尔值返回 NULL。

So receiving of POST parameter will look like:

所以接收 POST 参数将如下所示:

$isClass = filter_var ($_POST['isClass'], FILTER_VALIDATE_BOOLEAN);

回答by Nick Craver

You aren't doing anything wrong per se, it's just that when it gets posted, it looks like this:

你本身并没有做错什么,只是当它被发布时,它看起来像这样:

operation=add_cart&isClass=true&itemId=1234

PHP can't tell what the data type is because it isn't passed, it's always just a string of POST data, so compare it to "true"to do your checks, like this:

PHP 无法判断数据类型是什么,因为它没有传递,它始终只是一串 POST 数据,因此将其与"true"进行比较以进行检查,如下所示:

if($_POST['isClass'] === "true")
{
  //Code to add class to session cart
}
else
{
  //Code to add pack to session cart
}

回答by Sam Dufel

This is a bit of an old question, but I'm surprised nobody has posted this here as solution.

这是一个老问题,但我很惊讶没有人在这里发布这个作为解决方案。

Just use 1 and 0 instead of true and false when you're constructing your ajax requests. When you do a ==comparison, they'll be interpreted as true/false.

在构建 ajax 请求时,只需使用 1 和 0 而不是 true 和 false。当您进行==比较时,它们将被解释为真/假。

JS:

JS:

$.ajax({
  url: '....',
  data: {
    foo: 1,
    bar: 0
  }
});

PHP:

PHP:

<?php
  if ($_GET['foo']) {
     //...
  } else {
     //...
  }

  echo $_GET['bar'] ? 'bar is true' : 'bar is false';
?>

回答by OIS

  1. Send the data from your javascript as stringified JSON.
  2. Make a PHP function to convert the strings 'true' and 'false' to boolean value.
  1. 将 javascript 中的数据作为字符串化 JSON 发送。
  2. 制作一个 PHP 函数来将字符串 'true' 和 'false' 转换为布尔值。

Personally I like #2, which goes with Nick Craver's answer.

我个人喜欢#2,这与 Nick Craver 的回答相符。