【问题标题】:Returning a boolean true or false using the radio button?使用单选按钮返回布尔值 true 还是 false?
【发布时间】:2019-03-24 10:14:47
【问题描述】:

我有以下代码:

<input type="radio" name="mobileActivation" value="no" id="donotactivatemobile" onchange="disable_link();" onclick="checkBoxDisabling()" <?php if (isset($_POST['mobileActivation']) && $_POST['mobileActivation']=="no") $_POST['uclActivation'] = FALSE; ?> />

<input type="radio" name="mobileActivation" value="yes" id="activatemobile" onchange="enable_link();" onclick="checkBoxEnabling()"  <?php if (isset($_POST['mobileActivation']) && $_POST['mobileActivation']=="yes") $_POST['mobileActivation'] = TRUE;  ?> />

我似乎无法将返回值变成布尔值?我仍然得到值 yes 和 no :( 但不是 TRUE 或 false。有人可以帮忙吗?我已经尝试过这个 answer 但没有运气。

【问题讨论】:

    标签: php


    【解决方案1】:

    你可以使用这个技巧:

    <input type="radio" name="mobileActivation" value="0" /> false <br>
    <input type="radio" name="mobileActivation" value="1">   true  <br>
    

    然后您可以检查以下值:

    if(isset($_GET['mobileActivation'])) {
        $phone = (bool) $_GET['mobileActivation'];
        if($phone === true)
            echo("mobileActivation is true");
        else {
            echo("mobileActivation is false");
        }
    }
    

    这是因为 PHP 中的 0 被视为假布尔值,除 0 之外的所有其他整数都被视为真布尔值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 2013-11-09
      • 1970-01-01
      • 2014-03-23
      相关资源
      最近更新 更多