从单选按钮打印选中的值

时间:2012-12-18 12:15:37

标签: php

所以, 我有这个值:

$row['status'];

如何将选中的值标记为已检查? 这是形式:

<input type="radio"  id="status" name="status" value="1" /> Mbyllur<br />
<input type="radio"  id="status" name="status" value="0" /> Hapur<br />

因此,如果值为1,则应检查单选按钮。 感谢

6 个答案:

答案 0 :(得分:1)

<input type="radio"  id="status" name="status" <?php if($row['status'] == 1){ ?> checked="checked" <?php } ?> value="1" /> Mbyllur<br />
<input type="radio"  id="status" name="status" <?php if($row['status'] == 0){ ?> checked="checked" <?php } ?> value="0" /> Hapur<br />

答案 1 :(得分:1)

您应该检查变量是否先设置,或者您将获得Undefined variable: row - 错误,然后检查它的值:

<input type="radio"  id="status" name="status" <?php if(isset($row['status']) && $row['status'] == 1){ ?> checked="checked" <?php } ?> value="1" /> Mbyllur<br />
<input type="radio"  id="status2" name="status2" <?php if(isset($row['status2']) && $row['status2'] == 0){ ?> checked="checked" <?php } ?> value="0" /> Hapur<br />

编辑:您不应在页面上多次使用ID(“状态”)。 ID是唯一标识符。

答案 2 :(得分:0)

<input type="radio" id="status" name="status" value="1" <?=($row[ 'status' ] ? "checked" : "") ?>/>
<input type="radio" id="status" name="status" value="0" <?=($row[ 'status' ] ? "" : "checked") ?>/>

答案 3 :(得分:0)

这应该有效:

<input type="radio"  id="status" name="status" <?=($row['status'] == 1 ?"checked=\"checked\"" : "") ?> value="1" /> Mbyllur<br />
<input type="radio"  id="status" name="status" <?=($row['status'] == 0 ?"checked=\"checked\"" : "") ?> value="0" /> Hapur<br />

答案 4 :(得分:0)

if ($row['status'] == 1) { echo 'checked="checked"'; }

答案 5 :(得分:0)

克里斯的回答绝对正确,除了两个具有相同身份的单选按钮 对那些已经回答的人充分尊重,只是我的建议是页面中没有两个元素应该具有相同的id。