2013-03-20 61 views
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>"; 
+0

所以,當你做'in_array($ compi ['Competence_ID'],$ tmp)){'...你可以退出($ checked);'看看它是否在那裏設置?如果沒有...幾乎知道你的問題在哪裏.. – Ohgodwhy 2013-03-20 08:18:09

+0

嘗試在'checked'關鍵字周圍放置空格 – 2013-03-20 08:19:47

+0

謝謝Gareth!工作! – auicsc 2013-03-20 08:28:00

回答

2

必須有輸入名稱之間的空間,所以你需要添加一些空格,因爲HTML不知道解釋它(輸出爲:checkedname='c[]'):

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

太棒了!非常感謝 – auicsc 2013-03-20 08:28:31

0

試試這個:

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

似乎你錯過了:checked=".$checked

+0

沒有。你只需要打印'checked'。屬性的存在是它自身的假設。 – Ohgodwhy 2013-03-20 08:19:46

+0

他正在呼應'檢查'..其''檢查='缺少什麼 – gaurav 2013-03-20 08:21:35

+0

[再一次,先生,你錯了](http://jsfiddle.net/GSgCC/)。 – Ohgodwhy 2013-03-20 08:22:20

0

添加一個空格beween checkedname.

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