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"';} ?> />
Related
I'm stuck in a bit of a pickle I can't figure out. Any help is appreciated.
I set up an HTML radio table
<div class="radio-group">
<label class="heading">Choose your game</label><br/>
<table>
<tr>
<td>
<input type="radio" name="radio" value="Radio 1"> Radio 1
</td>
</tr>
<tr>
<td>
<input type="radio" name="radio" value="Radio 2"> Radio 2
</td>
</tr>
<tr>
<td>
<input type="radio" name="radio" value="Radio 3"> Radio 3
</td>
</tr>
<tr>
<td>
<input type="radio" name="radio" value="Radio 4"> Radio 4
</td>
</tr>
</table>
</div>
</br>
<input type="submit" name="submit" value="Submit" />
<input type="reset" name="reset" value="Reset"/>
</form>
</div>
My PHP page will POST the correct selection, but loads other stuff it shouldn't. How do I differentiate between the radio selections if there are all the same name? If I name it separately, you can click on every selection.
if (isset($_POST['submit'])) {
if(isset($_POST['radio']))
{echo '<iframe etc etc></iframe>;
}
else{echo '<iframe other></iframe>;}
}
You can check what value was posted and then make your echo statement based on that. Here's an example of two radio buttons in the same group and name
<input type="radio" name="radio" value="1"> Radio 1
<input type="radio" name="radio" value="2"> Radio 2
and here is the PHP to see which one has been submitted
if(isset($_POST['radio'])) {
$value = $_POST['radio'];
if($value == "1") {
echo "SOMETHING HERE FOR VALUE 1";
} elseif($value == "2") {
echo "SOMETHING HERE FOR VALUE 2";
}
}
Notice how the value is set to something simple like an integer, it makes it easier to check it later on and then display the appropriate or desired outcome for that selected button :)
this is how I have 2 checkbox and I do not want that you should only click on them simultaneously. it must only be possible to click on one at a time!. in the present to when I clicked the news, so it must enter a number in there to write "on". I would like to later be sure it is entered into the database.
the problem is that it appears to say "on" no matter what!?
<form action="#" method="post">
<table>
<tr>
<td>Emne</td>
<td>Vigtigt: <input type="checkbox" name="vigtigt" class="new"> Nyhede: <input type="checkbox" name="nyhede" class="new"></td>
</tr>
<tr>
<td>Title</td>
<td><input type="text" name="title" maxlength="50" class="new"></td>
</tr>
<tr>
<td>Tekst</td>
<td><textarea name="tekst" cols="20" rows="15" class="new"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="opret" value="Opret Blog" class="new"></td>
<td></td>
</tr>
</table>
<?php
if(isset($_POST["opret"]))
{
if($_POST["vigtigt"] != "")
{
echo $_POST["vigtigt"];
echo "<br />";
echo $_POST["nyhede"];
}
elseif ($_POST["nyhede"] != "")
{
echo $_POST["vigtigt"];
echo "<br />";
echo $_POST["nyhede"];
}
else
{
echo "Fejl!";
}
}
?>
</form>
You have to define a value attribute for it to:
<input type="checkbox" name="vigtigt" class="new" value="MyValueHere">
This way you will receive its value when its checked.
Edit:
To have only one selected at a time you will have to use type="radio" and give them the same name and different values.
<input type="radio" name="vigtigt" class="new" value="MyValueHere">
<input type="radio" name="vigtigt" class="new" value="MyOtherValueHere">
The purpose of checkboxes if to provide a group of options from which any number can be selected.
If you want a single option to be selected, use radio buttons (or a select element).
The value attribute (of the checked radio / selected option) determines the value that will be submitted. on is the default value for checkboxes which is used if you forget to specify a specific value.
<label><input type="checkbox" name="foo" value="vigtigt" class="new"> Vigtigt</label>
<label><input type="checkbox" name="foo" value="nyhede" class="new"> Nyhede</label>
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"'; ?> />
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>
The issue is I have a page with the following checboxes listed for a particular question.When I select one of the boxes and go to the next page and comeback then i find,none of the checkboxes appear to be checked.I have checked it on the back end and i was able to see that the checkboxes were indeed checked,but i was not able to view them as checked.I am not able to figure out as to why they dont appear to be checked.Any help regarding this would be appreciated.Thanks in Advance. The following is the code which i have in that page.
<td>
<input type="checkbox" name="test_na" value="N/A" <?=$test_na?> id="test_na">
<label for="test_na">NA</label>
</td>
<td>
<input type="checkbox" name="test_y" value="Y" <?=$test_y?> id="test_y">
<label for="test_y">Yes</label>
</td>
<td>
<input type="checkbox" name="test_n" value="N" <?=$test_n?> id="test_n">
<label for="test_n">No</label>
</td>
Test the value of the checkoxes and echo checked if value matches.
<td>
<input type="checkbox" name="test_na" value="N/A" <?php echo (isset($test_na) && $test_na == 'N/A' ? 'checked' : ''); ?> id="test_na">
<label for="test_na">NA</label>
</td>
<td>
<input type="checkbox" name="test_y" value="Y" <?php echo (isset($test_y) && $test_y == 'Y' ? 'checked' : ''); ?> id="test_y">
<label for="test_y">Yes</label>
</td>
<td>
<input type="checkbox" name="test_n" value="N" <?php echo (isset($test_n) && $test_n == 'N' ? 'checked' : ''); ?> id="test_n">
<label for="test_n">No</label>
</td>
See page source. What in your variables? It should be checked="checked" or checked="yes" or checked="1"