How to set the value for Radio Buttons When edit?

后端 未结 6 1684
忘了有多久
忘了有多久 2020-12-14 18:53

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

相关标签:
6条回答
  • 2020-12-14 19:05

    For those who might be in need for a solution in pug template engine and NodeJs back-end, you can use this:

    If values are not boolean(IE: true or false), code below works fine:

    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:

    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

    0 讨论(0)
  • 2020-12-14 19:13
    <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>
    
    0 讨论(0)
  • 2020-12-14 19:14

    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>
    
    0 讨论(0)
  • 2020-12-14 19:18
        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'];?>
    
    0 讨论(0)
  • 2020-12-14 19:26

    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

    The checked property will preselect the value that match

    0 讨论(0)
  • 2020-12-14 19:28

    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:

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

    0 讨论(0)
提交回复
热议问题