2009-06-09 131 views
0

我正在製作一個頁面,用戶可以在其中設置自己的設置。我需要一個循環來檢查該行是否爲真時的複選框,如果沒有,則需要取消選中該複選框。我將如何去做這件事?在php/javascript中。當mysql row = true時檢查複選框?

感謝

echo "<form method=\"post\">"; 

echo "<table> 
<tr> 
    <td>1</td> 
     <td><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\"></td> 
</tr> 

<tr> 
     <td>2</td> 
     <td><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\"></td> 
</tr> 


</table>"; 
echo"<input name=\"update\" type=\"submit\" id=\"update\" value=\"Update\" method\"post\">"; 
echo "</form>"; 

回答

1
while($row = mysql_fetch_assoc($rs)) 
{ 
    // some code... 

    $checked = ''; 
    if($row['setting_1'] === TRUE) 
    { 
      $checked = 'checked="checked"'; 
    } 

    echo '<input type="checkbox" name="setting_1" value="value_1" '.$checked.' />'; 

    // some code... 

} 
0

假設你從行的值,然後同時做迭代:

<input type="checkbox" <? if ($value==true) echo "checked=checked"; ?> /> 

PS。我只希望你不希望我們在這裏給你寫整個代碼,對吧?

+1

應該檢查=「檢查」或沒有,真/假是無效的HTML。 – 2009-06-09 18:43:43

+0

沒有我不是,只是爲了得到如何做到這一點:) – Elliott 2009-06-09 18:50:20

0

在你的循環,補充一點:

echo "<input type=\"checkbox\" "; 
if ($value_which_should_be_true) { echo "checked=\"checked\""; } 
echo "/>"; 

這用來複選框的checked HTML屬性,它指定默認狀態。

+0

如上所述,這應該檢查=「檢查」 – KOGI 2009-06-09 18:50:36

0

checked屬性以 「檢查」(見here),所以我會做這樣的事情:

<input type="checkbox" <? if ($value == true) echo 'checked="checked"'; ?> />

或者,你可以這樣做:

if ($value == true) { $checked = 'checked="checked"' }; 
echo '<input type="checked".$checked.' />; 
0

在我提交表格中我使用了

<input type="checkbox" name="check" id="check" value="checked" <?php $row['checkbox'];?> /> 

w當我從更新表單或循環調用數據時,上面的檢索和切換適用於每次更新或提交。希望這是有道理的或有幫助的。