41

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

The jquery part

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 -

//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:-

    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

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

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

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

Thanks, Sandeepan

4

4 回答 4

78

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

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

因此接收 POST 参数将如下所示:

$isClass = filter_var($_POST['isClass'], FILTER_VALIDATE_BOOLEAN);
于 2013-12-09T04:52:40.473 回答
57

您本身并没有做错任何事情,只是当它发布时,它看起来像这样:

operation=add_cart&isClass=true&itemId=1234

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

if($_POST['isClass'] === "true")
{
  //Code to add class to session cart
}
else
{
  //Code to add pack to session cart
}
于 2010-09-06T21:47:38.333 回答
19

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

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

JS:

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

PHP:

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

  echo $_GET['bar'] ? 'bar is true' : 'bar is false';
?>
于 2013-03-12T20:40:45.237 回答
1
  1. 从您的 javascript 中将数据作为字符串化 JSON 发送。
  2. 制作一个 PHP 函数将字符串 'true' 和 'false' 转换为布尔值。

就我个人而言,我喜欢#2,这与 Nick Craver 的回答相吻合。

于 2010-09-06T22:36:16.413 回答