auicsc
auicsc

Reputation: 297

Checkboxes do not get checked

I am trying to retrieve the checked checkboxes whose values are stored in the database, but they are always unchecked even if they exist in the DB. I have tried this but it did not work, although the same code with different structure works, but for framework reasons, i need to implement using this:

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

Upvotes: 1

Views: 104

Answers (3)

Mihai Iorga
Mihai Iorga

Reputation: 39724

There has to be spaces between input names, so you need to add some spaces because HTML does not know to interpret it (output is: checkedname='c[]'):

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

Upvotes: 2

Dipesh Parmar
Dipesh Parmar

Reputation: 27382

Add a space beween checked and name.

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

OR

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

Upvotes: 0

Gaurav Pandey
Gaurav Pandey

Reputation: 2796

Try this:

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

Seems you missed: checked=".$checked

Upvotes: 0

Related Questions