如何使用PHP显示以前选中的复选框

时间:2017-02-15 10:01:13

标签: php

如何显示选中的先前选中的复选框。 在我的表中,值存储如冰箱,空调,电视一栏

 <?php
    $answered="Refrigerator, Airconditioner, Television";
    $answer_options=array("Refrigerator","Airconditioner","tv");
    $ans_checked=explode(',',$answered);
    echo "<pre>";
    //print_r($ans_checked);
        $checked='';

    foreach($answer_options as $a)
    {
        echo $a."<br>";
        //print_r($ans_checked);
        if(in_array($a,$ans_checked))
        {
            $checked="checked";
        }
    //echo "$a<br>";
    echo "<input type='checkbox' ".$checked." >$a<br>";
    }
    ?>

2 个答案:

答案 0 :(得分:1)

我看到它正在检查此代码的所有复选框。一旦sin2()设置为&#34;已选中&#34;价值仍然存在。

如果不存在,您应该使用double sin2(double x) { bool neg = false; int ip = x / 2.0 / M_PI; x -= 2.0 * M_PI * ip; /* reduce to first period [-2PI..2PI] */ if (x < 0.0) x += 2.0*M_PI; /* reduce to first period [0..2PI] */ if (x > M_PI) { x -= M_PI; neg = true; } /* ... first period negative [ 0..PI ] */ if (x > M_PI/2.0) x = M_PI - x; /* reflection [0..PI/2] */ return neg ? MySin(-x) : MySin(x); } 部分取消设置。或者做 -

$checked

在循环中。

你也应该这样做 -

else

答案 1 :(得分:0)

<?php
    $answered="Refrigerator, Airconditioner, Television";
    $answered=str_replace(', ',',',$answered);
    $answer_options=array("Refrigerator","Airconditioner","tv");
    $ans_checked = explode(',', $answered);
    foreach ($answer_options as $a)
    {
        $checked = '';
        if (in_array($a, $ans_checked))
        {
            $checked = "checked";
        }
        echo $checked."<input type='checkbox' " . $checked . " >$a<br>";
    }
    ?>