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"'; ?> />
Related
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"';} ?> />
I have a the following form code:
<form method="post" action="nextpage">
<input type="radio" name="p" value="A"/>
<input type="radio" name="p" value="B"/>
<input type="radio" name="p" value="C"/>
<input type="radio" name="p" value="D"/>
</form>
The next page has to return the same form with the button checked.
this code: PHP How to keep radio button state to the next page didnt help.
<form method="post" action="nextpage.php">
<input type="radio" <?php if(isset($_POST['p'])) && $_POST['p'] == 'A') echo 'checked="checked" ';?> name="p" value="A"/>
//and so on for the rest....
You need to use the below code:
<form method="post" action="nextpage">
<input type="radio" name="p" value="A" <?php if(isset($_REQUEST['p']) && $_REQUEST['p'] == "A") echo 'checked="checked"'; ?>/>
<input type="radio" name="p" value="B" <?php if(isset($_REQUEST['p']) && $_REQUEST['p'] == "B") echo 'checked="checked"'; ?>/>
<input type="radio" name="p" value="C" <?php if(isset($_REQUEST['p']) && $_REQUEST['p'] == "C") echo 'checked="checked"'; ?>/>
<input type="radio" name="p" value="D" <?php if(isset($_REQUEST['p']) && $_REQUEST['p'] == "D") echo 'checked="checked"'; ?>/>
</form>
on the next page right above code.
As in your example answer there is radio button with the name q and in your code name is p that is different in both code. Hope this will be your problem.
<input type="radio" <?=(isset($_REQUEST['p']) && $_REQUEST['p'] == 'A') ? 'checked="checked" ' : ''?> name="p" value="A" />
I have here a numerous amount of radio buttons and their values are taken from a database. I want to add the type of leave but I can't figure it out. To make myself more clear, once the "Sick" radio button is clicked and when pressing the submit button, the "Sick" credits or the available hours for Sick leave (The value of my radio button which is the (value=$sLeave) and at the same time, the type of leave which is the Sick leave (Hidden type) will be passed to the next page.
I tried this one in every type of leave:
if($sLeave!=0)
{
?>
<input type="radio" name="optLeave" value="<?php echo $sLeave; ?>" onclick="disableTextarea()" /><label for="Sick">Sick</label>
<input type="hidden" name"leaveType" value="Sick" />
<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Sick">Sick</label>
<?php
}
?>
BUT all of the type of leave will be passed to the next page and it outputs the last hidden value. Not the one that is selected.
Here's the code:
<form id="leaveForm" name="lform" method="get" action="leaveFiled.php" />
if($sLeave!=0)
{
?>
<input type="radio" name="optLeave" value="<?php echo $sLeave; ?>" onclick="disableTextarea()" /><label for="Sick">Sick</label>
<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Sick">Sick</label>
<?php
}
?>
<?php
if($vLeave!=0)
{
?>
<input type="radio" name="optLeave" value="<?php echo $vLeave; ?>" onclick="disableTextarea()" /><label for="Vacation">Vacation</label>
<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Vacation">Vacation</label>
<?php
}
?>
<?php
if($eLeave!=0)
{
?>
<input type="radio" name="optLeave" value="<?php echo $eLeave; ?>" onclick="disableTextarea()" /><label for="Emergency">Emergency</label>
<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Emergency">Emergency</label>
<?php
}
?>
<?php
if($mLeave!=0 && $sex=='F')
{
?>
<input type="radio" name="optLeave" value="<?php echo $mLeave; ?>" onclick="disableTextarea()" /><label for="Maternity">Maternity</label>
<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Maternity">Maternity</label>
<?php
}
?>
<?php
if($pLeave!=0 && $sex=='M')
{
?>
<input type="radio" name="optLeave" value="<?php echo $pLeave; ?>" onclick="disableTextarea()" /><label for="Paternity">Paternity</label>
<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Paternity">Paternity</label>
<?php
}
?>
<?php
if($uLeave!=0)
{
?>
<input type="radio" name="optLeave" value="<?php echo $uLeave; ?>" onclick="disableTextarea()" /><label for="Union">Union</label>
<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Union">Union</label>
<?php
}
?>
<?php
if($oLeave!=0)
{
?>
<tr>
<td height="40" colspan="3" align="left">
<strong>
<input type="radio" name="optLeave" value="<?php echo $oLeave; ?>" onclick="enableTextarea()" />
<label for="Other Leave">Other Leave</label>
<label for="Reason of Leave">Reason of Leave:</label>
<input type="text" name="reasonLeave" size="35" disabled="disabled" />
</strong>
</td>
</tr>
<?php
}
else
{
?>
<tr>
<td height="40" colspan="3" align="left">
<strong>
<input type="radio" name="optLeave" disabled="disabled" />
<label for="Other Leave">Other Leave</label>
</strong>
</td>
</tr>
<?php
}
?>
Maybe the use of javascript may solve this problem but I'm hoping for another solution to this.
If you can use diffrent prefix with unique separator like underscore() or colon(:) whatever u want in values eg. sleave for sick leave or else
<input type="radio" name="optLeave" value="<?php echo "sleave_".$sLeave; ?>" onclick="disableTextarea()" /><label for="Sick">Sick</label>
<input type="radio" name="optLeave" value="<?php echo "uleave_".$uLeave; ?>" onclick="disableTextarea()" /><label for="Union">Union</label>
then in post u can separate the value by explode function and you will get the your values.
//use same separator here
$opt_leave = explode("_", $_REQUEST['optLeave']);
$leave_type = $opt_leave[0];
$leave = $opt_leave[1];
//now you can use $leave_type to check your conditions by switch or if conditional statements
if($leave_type == 'sleave') {
// action on sick leave
} else if($leave_type == 'uleave') {
// action on Union leave
}
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>
To allow a user to edit information in a record, this is done:
$case=$_GET['case'];
$query="SELECT * FROM `cases` WHERE `case`= '$case'";
$result=mysql_query($query);
<input type="text" name="firstname" value="<?php echo $firstname; ?>" />
I need to set the value of a radio group based on what its value is in the "cases" table.
<input type="radio" name="flight1_departing" value="AM" />
<input type="radio" name="flight1_departing" value="PM" />
How is this possible?
<input <?php if ($somevalue == 'AM') echo 'checked="checked"'; ?> type="radio" name="flight1_departing" value="AM" />
<input <?php if ($somevalue == 'PM') echo 'checked="checked"'; ?> type="radio" name="flight1_departing" value="PM" />
Given a known value $val, you just need to check it against each radio button value and set the checked attribute, eg
<input type="radio" name="flight1_departing" value="AM"
<?php if ($val == 'AM') : ?>checked="checked"<?php endif ?>
/>
<input type="radio" name="flight1_departing" value="PM"
<?php if ($val == 'PM') : ?>checked="checked"<?php endif ?>
/>
That example is very manual. It would be easier if the radio elements are created in a loop.
Your questions is a little ambiguous but I'm going on the assumption that you mean you need to determine which value is default checked based on the value in the cases table?
Something like,
<input type="radio" name="flight1_departing" value="AM" <?php if ($some_cases_value) { print 'CHECKED'; } ?>/>
<input type="radio" name="flight1_departing" value="PM" <?php if ($some_cases_value) { print 'CHECKED'; } ?> />
Though there is likely a very more elegant way of doing it?