1

我正在尝试检索其值存储在数据库中的选中复选框,但即使它们存在于数据库中,它们也始终未选中。我已经尝试过了,但它不起作用,虽然具有不同结构的相同代码可以工作,但出于框架原因,我需要使用它来实现:

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>";
4

3 回答 3

2

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

if(in_array($compi['Competence_ID'],$tmp)){
    $checked = ' checked ';
}
于 2013-03-20T08:24:05.640 回答
0

试试这个:

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

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

于 2013-03-20T08:19:02.347 回答
0

checked在和之间添加一个空格name.

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>";
于 2013-03-20T08:22:38.023 回答