php - checkboxes checked when mysql row = true? - Stack Overflow

I am making a page where the user can set there own settings. I need a loop to check the checkbox when

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
Add a ment  | 

6 Answers 6

Reset to default 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...

}

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745228576a4617575.html

相关推荐

  • php - checkboxes checked when mysql row = true? - Stack Overflow

    I am making a page where the user can set there own settings. I need a loop to check the checkbox when

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信