Skip to content Skip to sidebar Skip to footer

How To Set The Value For Radio Buttons When Edit?

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 aga

Solution 1:

When you populate your fields, you can check for the value:

<inputtype="radio"name="sex"value="Male"<?phpecho ($sex=='Male')?'checked':''?>size="17">Male
<inputtype="radio"name="sex"value="Female"<?phpecho ($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

Solution 2:

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:

<inputtype="radio"name="sex"value="Male"size="17"<?phpecho($isMale?'checked="checked"':''); ?>>Male
<inputtype="radio"name="sex"value="Female"size="17"<?phpecho($isFemale?'checked="checked"':''); ?>>Female

in this example $isMale & $isFemale is boolean values that you assign based on the value from your database.

Solution 3:

This is easier to read for me:

<inputtype="radio"name="rWF"id="rWF"value=1<?phpif ($WF == '1') {echo' checked ';} ?> />Water Fall</label><inputtype="radio"name="rWF"id="rWF"value=0<?phpif ($WF == '0') {echo' checked ';} ?> />nope</label>

Solution 4:

    Gender :<br><inputtype="radio"name="g"value="male"<?phpecho ($g=='Male')?'checked':''?>>male <br><inputtype="radio"name="g"value="female"<?phpecho ($g=='female')?'checked':''?>>female
            <?phpecho$errors['g'];?>

Solution 5:

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

Post a Comment for "How To Set The Value For Radio Buttons When Edit?"