复选框不会被检查

时间:2013-03-20 08:15:12

标签: php html

我正在尝试检索其值存储在数据库中的已选中复选框,但即使它们存在于数据库中,也始终取消选中它们。 我尝试过这个但它没有用,虽然不同结构的相同代码有效,但出于框架原因,我需要使用这个来实现:

if(isset($sp['Etunimi'])&&isset($sp['Sukunimi'])){
        echo "<form method='post' action=''>";
        $comp=$this->All_Competences;
        echo"<br/>select competences for:".$sp['Etunimi'];
        $id=$sp['Id'];
        $tmp=array();
        if(isset($_POST['select_employee'])){
            $cid=$this->cids;
        }
        foreach($cid as $test)
        {
            array_push($tmp, $test['c_ID']);
        }
        for($i=0;$i<count($tmp);$i++){
        }
        echo "<table><th>valid?</th><th>Competence description</th>";

        foreach($comp as $compi){
            $checked='';
            if(in_array($compi['Competence_ID'],$tmp)){
                $checked='checked';


            }
            echo "<tr><td><input type='checkbox'".$checked."name='c[]' value='".$compi['Competence_ID']."'></td><td>".$compi['Competence_Description']."</td></tr>";

        }   
        echo "</table>";
        echo "<input type='hidden' name='action' value='selectchecked'>";
        echo "<input type='hidden' name='id' value='".$id."'>";
        echo "<input type='submit' value='submit checks'>";
        echo "</form>";

3 个答案:

答案 0 :(得分:2)

输入名称之间必须有空格,因此您需要添加一些空格,因为HTML无法解释它(输出为:checkedname='c[]'):

if(in_array($compi['Competence_ID'],$tmp)){
    $checked = ' checked ';
}

答案 1 :(得分:0)

试试这个:

 echo "<tr><td><input type='checkbox' checked='".$checked."' name='c[]' value='".$compi['Competence_ID']."'></td><td>".$compi['Competence_Description']."</td></tr>";

似乎你错过了:checked=".$checked

答案 2 :(得分:0)

checkedname.

之间添加空格
echo "<tr><td><input type='checkbox' checked='".$checked."' name='c[]' value='".$compi['Competence_ID']."'></td><td>".$compi['Competence_Description']."</td></tr>";

echo "<tr><td><input type='checkbox' ".$checked." name='c[]' value='".$compi['Competence_ID']."'></td><td>".$compi['Competence_Description']."</td></tr>";