开发者

How could I display a checked checkbox based from it's value in the database?

开发者 https://www.devze.com 2023-02-06 15:16 出处:网络
I\'m new in PHP, could you help me s开发者_如何学JAVAolve my problem? I\'m trying to display the checkbox checked based on its value in the database. I have saved its value as 1 if it\'s checked and 0

I'm new in PHP, could you help me s开发者_如何学JAVAolve my problem? I'm trying to display the checkbox checked based on its value in the database. I have saved its value as 1 if it's checked and 0 if it's not.


<?php

$sql = "SELECT somecol FROM sometable";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$checked = $result['somecol'];

?>

<input type="checkbox" name="somecol" value="1" <?php if ($checked == 1) echo 'checked'; ?> />


You can test the field, let's say $row['col'], and emit checked="checked" if the field contains 1.

echo '<input type="checkbox" name="n" value="v"' . ($row['col']==1 ? ' checked="checked"' : '') . '>';
  • Use checked="checked"
0

精彩评论

暂无评论...
验证码 换一张
取 消