php 编辑时如何设置单选按钮的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6197377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the value for Radio Buttons When edit?
提问by ssss05
I have a Gender Row with radio buttons male & female. when i register as first time the values of radio button will store in database. Now my question is if i edit that row again it want to come(that means checked) with that value as male/female. how to make it?
我有一个带有男性和女性单选按钮的性别行。当我第一次注册时,单选按钮的值将存储在数据库中。现在我的问题是,如果我再次编辑该行,它想以该值作为男性/女性出现(这意味着已检查)。怎么做?
Note : Doing with php.
注意:用php做。
HTML Script :
HTML 脚本:
<tr id="inside">
<td align="right" width="40%" id="side" >Gender</td>
<td width="3%"> </td>
<td align="left" width="50%">
<input type="radio" name="sex" value="Male" size="17">Male
<input type="radio" name="sex" value="Female" size="17">Female
</td>
</tr>
回答by Ibu
When you populate your fields, you can check for the value:
填充字段时,您可以检查值:
<input type="radio" name="sex" value="Male" <?php echo ($sex=='Male')?'checked':'' ?>size="17">Male
<input type="radio" name="sex" value="Female" <?php echo ($sex=='Female')?'checked':'' ?> size="17">Female
Assuming that the value you return from your database is in the variable $sex
假设您从数据库返回的值在变量中 $sex
The checked property will preselect the value that match
选中的属性将预选匹配的值
回答by Nightwolf
just add 'checked="checked"' in the correct radio button that you would like it to be default on. As example you could use php quick if notation to add that in:
只需在正确的单选按钮中添加 'checked="checked"' 您希望它是默认的。例如,您可以使用 php quick if 符号将其添加到:
<input type="radio" name="sex" value="Male" size="17" <?php echo($isMale?'checked="checked"':''); ?>>Male
<input type="radio" name="sex" value="Female" size="17" <?php echo($isFemale?'checked="checked"':''); ?>>Female
in this example $isMale & $isFemale is boolean values that you assign based on the value from your database.
在本例中,$isMale 和 $isFemale 是您根据数据库中的值分配的布尔值。
回答by JustJohn
This is easier to read for me:
这对我来说更容易阅读:
<input type="radio" name="rWF" id="rWF" value=1 <?php if ($WF == '1') {echo ' checked ';} ?> />Water Fall</label>
<input type="radio" name="rWF" id="rWF" value=0 <?php if ($WF == '0') {echo ' checked ';} ?> />nope</label>
回答by user8092150
Gender :<br>
<input type="radio" name="g" value="male" <?php echo ($g=='Male')?'checked':'' ?>>male <br>
<input type="radio" name="g" value="female"<?php echo ($g=='female')?'checked':'' ?>>female
<?php echo $errors['g'];?>
回答by Prince Bassey
For those who might be in need for a solution in pug template engine and NodeJs back-end, you can use this:
对于那些可能需要 pug 模板引擎和 NodeJs 后端解决方案的人,您可以使用:
If values are not boolean(IE: true or false), code below works fine:
如果值不是布尔值(IE:true 或 false),下面的代码工作正常:
input(type='radio' name='sex' value='male' checked=(dbResult.sex ==='male') || (dbResult.sex === 'newvalue') )
input(type='radio' name='sex' value='female' checked=(dbResult.sex ==='female) || (dbResult.sex === 'newvalue'))
If values are boolean(ie: true or false), use this instead:
如果值为布尔值(即:true 或 false),请改用:
input(type='radio' name='isInsurable' value='true' checked=singleModel.isInsurable || (singleModel.isInsurable === 'true') )
input(type='radio' name='isInsurable' value='false' checked=!singleModel.isInsurable || (singleModel.isInsurable === 'false'))
the reason for this || operator is to re-display new values if editing fails due to validation error and you have a logic to send back the new values to your front-end
这样做的原因 || 如果由于验证错误而导致编辑失败并且您有将新值发送回前端的逻辑,则运算符将重新显示新值
回答by Om Prakash
<td><input type="radio" name="gender" value="Male" id="male" <? if($gender=='Male')
{?> checked="" <? }?>/>Male
<input type="radio" name="gender" value="Female" id="female" <? if($gender=='Female') {?> checked="" <?}?>/>Female<br/> </td>