admin管理员组文章数量:1415139
I am making a page where the user can set there own settings. I need a loop to check the checkbox when the row is true and to be unchecked when its not. How would I go about this? in php/javascript.
Thanks
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>";
I am making a page where the user can set there own settings. I need a loop to check the checkbox when the row is true and to be unchecked when its not. How would I go about this? in php/javascript.
Thanks
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>";
Share
Improve this question
edited Jun 9, 2009 at 18:42
Elliott
asked Jun 9, 2009 at 18:36
ElliottElliott
3,87424 gold badges71 silver badges93 bronze badges
0
6 Answers
Reset to default 3while($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...
}
The checked
attribute takes "checked" (see here), so I'd do something like:
<input type="checkbox" <? if ($value == true) echo 'checked="checked"'; ?> />
Alternatively, you can do something like:
if ($value == true) { $checked = 'checked="checked"' };
echo '<input type="checked".$checked.' />;
In your loop, add this:
echo "<input type=\"checkbox\" ";
if ( $value_which_should_be_true ) { echo "checked=\"checked\""; }
echo "/>";
This uses the checked
HTML attribute for checkboxes, which specifies the default state.
Suppose you get a value from row and then while iterating do:
<input type="checkbox" <? if ($value==true) echo "checked=checked"; ?> />
PS. I just hope you are not expecting us to wrote here the entire whole code for you, right?
In my submit form I used
<input type="checkbox" name="check" id="check" value="checked" <?php $row['checkbox'];?> />
wWhen I call the data from an update form or loop the above retrieves and the toggle works with every update or submission. Hope this makes sense or helps.
<input <?php if (!(strcmp($row->value,1))) {echo "checked=\"checked\"";} ?> name="ckeck" id="check" type="checkbox" value="1">
本文标签: phpcheckboxes checked when mysql rowtrueStack Overflow
版权声明:本文标题:php - checkboxes checked when mysql row = true? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745228576a2648721.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论