duandun3178 2014-04-21 20:28
浏览 22
已采纳

我有两个类似的功能,一个输出改变而另一个没有

This code uses html, php and laravel. The first one 'tk' doesn't change in value whatever i do. The second one 'ntk' does change the value but eventually my output on the webpage allways gives me checked checkboxes for some reason. Really hope someone could help me out because I really don't know how this is possible. Especially that when I enter false in $ntk than the checkbox stays unchecked.

Here's my form:

<?php
        $tk = Session::has('tk') ? Session::get('tk') : 1;
        var_dump($tk);
        if($tk == 1){
            $tk = 'true';
        } else {
            $tk = 'false';
        }
        $tk_name = 'tk: ';
        var_dump($tk_name);
        var_dump($tk);
        $ntk = Session::has('ntk') ? Session::get('ntk') : 0;
        var_dump($ntk);
        if($ntk == 1){
            $ntk = 'true';
        } else {
            $ntk = 'false';
        }
        $ntk_name = 'ntk: ';
        var_dump($ntk_name);
        var_dump($ntk);
    ?>
    {{ Form::open(array('url' => 'aanvullen')) }}
        <table>
            <tr>
                <td>
                    {{ Form::submit('Toepassen') }}
                </td>
                <td>
                    {{ Form::checkbox('tk', 1, $tk) }} Tijdkritisch (TK) <br>
                    {{ Form::checkbox('ntk', 1, $ntk) }} Niet-tijdkritisch (NTK)
                <td>
            </tr>
        </table>
    {{ Form::close() }}

Here's my controller code:

if(isset($_POST['tk']) )
    {
        $tk = $_POST['tk'];
        Session::put('tk', $tk);
    } else {
        $tk = 1;
        Session::put('tk', $tk);
    }

    if(isset($_POST['ntk']) )
    {
        $ntk = $_POST['ntk'];
        Session::put('ntk', $ntk);
    } else {
        $ntk = 0;
        Session::put('ntk', $ntk);
    }
  • 写回答

1条回答 默认 最新

  • dongyudun6465 2014-04-21 22:12
    关注

    You are using this:

    if($tk == 1){
        $tk = 'true';
    } else {
        $tk = 'false';
    }
    

    In this case, $tk is always true because true is String not a Boolean value and when you use $tk as Boolean value, it comes out to true because it's a truthy value, for example:

    $tk = 'false';
    echo !!$tk; // 1 = true
    

    Anyways, that's the problem, your $tk contains a string not Boolean and the right way to assign a Boolean value is:

    $k = TRUE; / without qoutes
    

    But you don't have to assign TRUE or FALSE to $tk like this:

    enter code hereif($tk == 1){
        $tk = true;
    } else {
        $tk = false;
    }
    

    Since, 0 is a falsy value evaluates to FALSE and 1 evaluates to TRUE so you can directly use $tk like this:

    {{ Form::checkbox('tk', 1, $tk) }}
    

    Depending on the value (1 or 0) of $tk it would be either:

    {{ Form::checkbox('tk', 1, 1) }} // Checked
    {{ Form::checkbox('tk', 1, 0) }} // Unchecked
    

    So, if the $tk contains 1 then the checkbox will be checked and if 0 then it won't be checked.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值