假设我们及时将数据插入数据库有 3 个 input:radio
并从中选择一个,将其值插入数据库。现在如何从数据库中选择与插入数据库中相同的 radio 进行检查。
示例:
我们有 3 个 input:radio
为:
<input type="radio" name="type1" value="value1">
<input type="radio" name="type2" value="value2" checked>
<input type="radio" name="type3" value="value3">
选中value2
后将其插入数据库,现在我们要显示(select
)所有 radio 并检查插入数据库的 radio 。as(这是在选择之后来自数据库):
<input type="radio" name="type1" value="value1">
<input type="radio" name="type2" value="value2" checked> // this value was in the database
<input type="radio" name="type3" value="value3">
如何使用 PHP 修复它?
最佳答案
正如前面所说,您应该对单选按钮使用相同的名称。另外,正确的语法是 checked="checked"
:
<input type="radio" name="type" value="value1">Value 1
<input type="radio" name="type" value="value2" checked="checked">Value 2
<input type="radio" name="type" value="value3">Value 3
从数据库检索时,您可以检查值是否等于 html 中的值,并相应地设置检查属性。
<input type="radio" name="type" value="value1" <?php echo ($query_hs->type == 'value1') ? 'checked="checked"' :'';?>>Value 1
<input type="radio" name="type" value="value2" <?php echo ($query_hs->type == 'value2') ? 'checked="checked"' :'';?>>Value 2
<input type="radio" name="type" value="value3" <?php echo ($query_hs->type == 'value3') ? 'checked="checked"' :'';?>>Value 3
只需用我的标准值替换您自己的值,例如:
<input type="radio" name="type" value="hotel" <?php echo ($query_hs->type == 'hotel') ? 'checked="checked"' :'';?>>Hotel
关于php - 检查选择时插入数据库的 radio ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7671003/