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>
Related
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 a table with the fields name and gender. I get the data and I try to display the information in the input fields, and it works on the input text Name, but i wonder if i can check the radio buttons depending of the data i get from the table?
<?php
$sql="select * from empleo where id='$id';";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));}
$cs=mysqli_query($con,$sql);
while($resul=mysqli_fetch_array($cs)){
$name=$resul[0];
$sexo=$resul[1];
}
?>
Name: <input type="text" value="<?php echo $name?>"/><br>
<label for="radio">Hombre</label>
<input type="radio" name="gender" id="radio" value="men">
<label for="radio" > Mujer</label>
<input type="radio" name="gender" id="radio" value="women">
Supposing your $result[1] is W for women and M for men (change the code, if not):
<label for="radio">Hombre</label>
<input type="radio" name="gender" id="radio" value="men" <?php if($sexo=="M") echo "checked" ?>>
<label for="radio" > Mujer</label>
<input type="radio" name="gender" id="radio2" value="women" <?php if($sexo=="W") echo "checked" ?>>
Read Radio Button Input
<label for="sexo-men">Hombre</label>
<input type="radio" name="gender" id="sexo-men" value="men" <?php $sexio === 'men' ? 'checked="checked"' : '' ?> />
<label for="sexo-women">Mujer</label>
<input type="radio" name="gender" id="sexo-women" value="women" <?php $sexio === 'women' ? 'checked="checked"' : '' ?> />
And also id attribute of html is must unique on the page.
Checking a checkbox in HTML is done with the checked attribute:
<input name="awesome" type="checkbox" checked="checked" />
So we can setup a little function to help us output this:
function check_if($condition) {
if ($condition) {
echo 'checked="checked"';
}
}
The problem with your script is that you only set up variables for your first record from your empleo table.
What you most likely want is to instead loop through all the records:
// Lets get all the records from result and as an associative array
$employees = mysqli_fetch_all($cs, MYSQLI_ASSOC);
// this lets us use column names instead of indexes - much easier to read the code.
foreach($employees as $e) { ?>
<label for="name">Name<label><input name="name" type="text" value="<?php echo $e['name']?>"/>
<label for="gender">Hombre</label>
<input type="radio" name="gender" id="radio" value="male" <?php check_if($e['gender'] === 'male') ?>>
<label for="gender">Mujer</label>
<input type="radio" name="gender" id="radio" value="female" <?php check_if($e['gender'] === 'female') ?>>
<?php } // end foreach ?>
I assumed the columns in your empleo table where gender and name.
You also should note that each <input> should have a name attribute and each <label> should have a for
attribute which matches the name of the input it labels.
I have a form with a radio button group. I want to set 'checked' as default the second radio button, and also keep the value after submitting form if user clicks on the first one.
Note that I'm using the <span class="custom-radiobutton"></span> for style the radiobuttons, with a lower z-index than the real <input type="radio", which has opacity:0.
This is a snippet of my code:
<div class="group-wrapper">
<div class="radiobutton-wrapper boolean">
<span class="custom-radiobutton"></span>
<input type="radio" id="hosting-1" name="hosting[]" value="1" class="w100" <?php if ((isset($_POST['hosting'])) && ((isset($_POST['hosting'])) == 1)) {echo 'checked="checked"';}; ?> />
<label for="hosting-1">Sí</span>
</div>
<div class="radiobutton-wrapper boolean">
<span class="custom-radiobutton"></span>
<input type="radio" id="hosting-2" name="hosting[]" value="0" class="w100" <?php if ((!isset($_POST['hosting'])) || ((isset($_POST['hosting'])) == 0)) {echo 'checked="checked"';}; ?> />
<label for="hosting-2">No</label>
</div>
</div>
Additional info:
I'm using HTML5.
I'm validating the form with PHP (I want to keep this, even if I know maybe is better jQuery+PHP validation).
I have noticed that I need two clicks to select the first radio button. This only occurs from original state. After this, It works with one click, as expected.
I'm expending a lot of hours trying to figure out what's wrong, so any help will be very appreciated.
Cheers,
<input type="radio" id="hosting-1" name="hosting[]" class="w100" checked="checked" /> 1
try this in your code.
for example:
<tr>
<td><br><b>Reservation Handling : <span style="color:#FF0000">* </span></td>
<td><br><input type="radio" name="reservation" value="Delighted" required> Delighted<br></input></td>
</tr>
<tr>
<td> </td>
<td><input type="radio" name="reservation" checked="checked" value="Satisfied"> Satisfied</input></td>
</tr>
<tr>
<td> </td>
<td><input type="radio" name="reservation" value="Dissatisfied"> Dissatisfied</input></td>
</tr>
<tr>
<td> </td>
<td><input type="radio" name="reservation" value="N/A"> N/A</input></td>
</tr>
You have an error in your logic. You're using isset twice.
<input type="radio" id="hosting-1" name="hosting[]" value="1" class="w100" <?php if ((isset($_POST['hosting'])) && ((isset($_POST['hosting'])) == 1)) {echo 'checked="checked"';}; ?> />
You're essentially saying does isset equal 1, which in your case will always be true if $_POST['hosting'] has a value.
You should do this
<input type="radio" id="hosting-1" name="hosting[]" value="1" class="w100" <?php if (isset($_POST['hosting']) && $_POST['hosting'] == 1) {echo 'checked="checked"';} ?> />
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
Form table is introduced inside an echo and, for the texts fields, I use value=" ' .$_POST['name'] to set default value if form was already sent at least one time. It works properly.
However, how could I save radio buttons status when form was already sent? Thanks.
<tr>
<td colspan="2" align="left" valign="top"> <input type="radio" name="ambiente" value="si" />
Si
<input type="radio" name="ambiente" value="no" />
No</td>
</tr>
Simply:
<input type="radio" name="ambiente" value="si" <?php if ($_POST['ambiente'] == 'si') echo 'checked'; ?> /> Si
<input type="radio" name="ambiente" value="no" <?php if ($_POST['ambiente'] == 'no') echo 'checked'; ?> /> No
<?php
$checked = NULL;
if(isset($_POST['ambiente']) && $_POST['ambiente'] == 'no') {
$checked = 'checked="checked"';
}
?>
<input type="radio" name="ambiente" value="no" <?php echo $checked ?> />
Or this can be shown on one line:
<input type="radio" name="ambiente" value="no" <?php if (isset($_POST['ambiente']) && $_POST['ambiente'] == 'no') echo 'checked="checked"'; ?> />