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
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
<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>
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>
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'];?>
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
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.