CODE am trying:
<input type="radio" name="type" value="Male" <?php if ($type == 'Male') echo 'checked="checked"'; ?>"> Male
OR
<input type="radio" name="gender" value="<?php echo $gender ?>"/>Female
This one is right ??
i refer 2 links in SO but m not getting properly with radio button ....
Suggestions always Welcome ...
The first one is correct:
<input type="radio" name="type" value="Male" <?php if ($type == 'Male') echo 'checked="checked"'; ?>"> Male
Related
Open a div on based on database radio button value. if value is male in database then show For male and if value is female in database then show Female male
$status = isset($_POST['status']) ? $_POST['status'] : '';
<input type="radio" name="status" value="male" checked="checked">male<br>
<input type="radio" name="status" value="female">female<br>
<div class="1">For male</div>
<div class="2">Female male</div>
if i understand you right this will do the trick
$status = isset($_POST['status']) ? $_POST['status'] : '';
if ($status == "male") {
<input type="radio" name="status" value="male" checked="checked">male<br>
<input type="radio" name="status" value="female">female<br>
}elseif ($status == "female") {
<input type="radio" name="status" value="male">male<br>
<input type="radio" name="status" value="female" checked="checked">female<br>
}else {
<input type="radio" name="status" value="male">male<br>
<input type="radio" name="status" value="female">female<br>
}
Just add if condition
$status = isset($_POST['status']) ? $_POST['status'] : ''; ?>
<input type="radio" name="status" value="male" <?php echo $status=='male'? 'checked="checked"':''?>>male<br>
<input type="radio" name="status" value="female" <?php echo $status=='female'? 'checked="checked"':''?>>female<br>
<?php if($status=='male') { ?>
<div class="1">For male</div>
<?php } ?>
<?php if($status=='female') { ?>
<div class="2">For Female</div>
<?php } ?>
This question already has answers here:
How to check database and, as a result, check a radio button?
(3 answers)
Closed 3 years ago.
I have one radio button of gender. The value is getting stored in db and i am able to fetch it too. But I want the radio button to be checked for the received value. Help me.
<input type="radio" name="gender" value="male" > Male
<input type="radio" name="gender" value="female" checked> Female
You can make it work as follow
<?php
$db_value = 'male'; // this is the value from db
?>
<input type="radio" name="gender" value="male" <?php if($db_value == 'male'):?>checked<?php endif;?>> Male
<input type="radio" name="gender" value="female" <?php if($db_value == 'female'):?>checked<?php endif;?>> Female
You can check which radiobutton should be selected by using if-else statement like below where $row['gender'] is column where radiobutton value is there :
<?php if($row['gender']== 'male') { ?>
<input type="radio" name="gender" value="male" checked> Male
<?php }else { ?>
<input type="radio" name="gender" value="female" checked> Female
<?php } ?>
I searched for the answer everywhere, although similar questions are already there on stackoverflow i did not find a satisfactory answer.
Following are the question i've referred,
checked the radio button based on the ajax response
Check the radio button based on the value fetched from mysql database
What i have is data coming in from model in object $data.
So i fetch it in my view like $data->is_male.
What i want is if($data->is_male == 1) then check the button,
<input type="radio" name="gender" value=
<?php if ($data->is_male == 1): ?>
'checked'
<?php endif ?>> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
Try this:-
<input type="radio" name="gender" value="male"
<?php if ($data->$data->is_male == 1): ?>
checked="checked"
<?php endif ?>> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
Below is a more readable answer. I have used PHP ternary conditional operator here.
<input type="radio" name="gender" value="male" <?php echo ($data->is_male==1) ? 'checked="checked"':'';?>>Male
<br>
<input type="radio" name="gender" value="female"> Female
<br>
I have these profiles saved in a database with lots of random information like name, gender, age, etc.
The user can edit their profile by clicking a hyperlink and it takes them to a form much like the one they filled out when they first registered. I've designed it so all the text input fields already have the values they had previously filled in. Here's an example of what one looks like:
<input type="text" name="fname" value="<?php echo $result['firstName']; ?>"/>
As you can see I'm echoing their name from the array I created from querying the database. But the problem is I used radio buttons (and drop down boxes too) for some of these inputs like gender.
So how do I check the appropriate radio button once I establish they're male or female from the database?
Very easily:
<input type="radio" name="gender" value="M" <?php echo ($result['gender'] == "M" ? 'checked="checked"': ''); ?> />
<input type="radio" name="gender" value="F" <?php echo ($result['gender'] == "F" ? 'checked="checked"': ''); ?> />
Same thing for a select box, when listing the options check and see if their stored value is the same as the value of the option you're listing.
<input type="radio" name="gender" value="m" <? if($row['gender'] == "m") print "selected";?> >
<input type="radio" name="gender" value="f" <? if($row['gender'] == "f") print "selected";?> >
i think it's simplier. because they can;t be both, at the same time.
something like this:
if($result['gender'] == 'male')
{
echo '<input type="radio" name="gender" value="male" checked="checked"> Male';
echo '<input type="radio" name="gender" value="female"> Female';
}
else {
echo '<input type="radio" name="gender" value="male"> Male';
echo '<input type="radio" name="gender" value="female" checked="checked"> Female';
}
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.
HTML Script :
<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>
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.
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'];?>
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
If you are getting your values from a database table and creating radio buttons dynamically, here is the solution. The database records are fetched into an array in this example and used for creating radio buttons.
<?php foreach ($dbrecords as $item) : ?>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="paymentMethod" id="paymentMethod" value=<?php echo $item["Id"]; ?> <?php echo ($paymentMethod == $item["Id"]) ? 'checked' : '' ?>><?php echo $item["Name"]; ?>
</div>
<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>