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

前端 未结 4 1702
南方客
南方客 2020-12-03 00:41

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

The jquery part

function addClassToCart(item         


        
4条回答
  •  庸人自扰
    2020-12-03 01:18

    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:

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

提交回复
热议问题