mysql row = true时检查复选框?

问题描述 投票:0回答:6

我正在创建一个页面,用户可以在其中设置自己的设置。当行为真时我需要一个循环来检查复选框,当它不是时我需要取消选中。我该怎么做?在php / javascript中。

谢谢

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>";
php javascript
6个回答
3
投票
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
投票

checked属性需要“检查”(参见here),所以我会做类似的事情:

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

或者,您可以执行以下操作:

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

0
投票

在你的循环中,添加:

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

这使用checked HTML属性作为复选框,指定默认状态。


0
投票

假设您从行获取值,然后在迭代时执行:

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

PS。我只是希望你不要指望我们在这里为你写完整个代码,对吧?


0
投票

在我提交的表格中,我用过

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

当我从更新表单中调用数据或循环上面的检索时,切换适用于每次更新或提交。希望这有意义或有帮助。


0
投票
<input <?php if (!(strcmp($row->value,1))) {echo "checked=\"checked\"";} ?> name="ckeck" id="check" type="checkbox" value="1">
© www.soinside.com 2019 - 2024. All rights reserved.