Form with radio button - php

I have this:
<td>
<input id="sex" name="sexFemale" value="female" type="radio">
<label for="sexFemale">
Kvinna
</label>
</td>
<td>
<input id="sex" name="sexBoth" value="both" checked="checked" type="radio">
<label for="sexBoth">
Båda
</label>
</td>
<td>
<input id="sex" name="sexMale" value="male" type="radio">
<label for="sexMale">
Man
</label>
</td>
I think I made this wrong, how should I use it?
$_POST["sex"] to get the value "male" or "female" or what they chosed

You should set their name attribute to "sex" and have them carry diffrerent values:
<input type="radio" name="sex" value="female" />Female<br />
<input type="radio" name="sex" value="male" />Male<br />

the name of the radiobuttons has to be the same, not the id (the id must be unique)

You need to give them the same name (the name of the radio button group) attribute and different id attributes to work:
<td>
<label><input id="sexFemale" name="groupSex" value="female" type="radio">
Kvinna</label>
</td>
<td>
<label><input id="sexBoth" name="groupSex" value="both" checked="checked" type="radio">
Båda</label>
</td>
<td>
<label><input id="sexMale" name="groupSex" value="male" type="radio">
Man</label>
</td>

i think than all radio button inputs in one group must have the same 'name' attribute:
Kvinna
Båda
Man

$_POST['sex'] returns are element value with name attribute "sex". For use it - set all radiobutons name to "sex".

Just switch your name attributes with your id attributes. So instead of
<input id="sex" name="sexFemale" ...
just use
<input id="sexFemale" name="sex" ...

Related

PHP form arrays not received post

I have a form, but I can not receive the data correctly.
I want to receive the id and the option you chose, how can I do this?
I tried to foreach, but could not.
<input type="hidden" id="Id_field[]" name="Id_field[]" value="1"/>
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="yes" />Yes
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="no" />No
<input type="hidden" id="Id_field[]" name="Id_field[]" value="2"/>
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="yes" />Yes
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="no" />No
Receiving correctly:
Id_field = 1
option = yes
Id_field = 2
option = no
First of all, id's need to be unique, so this isn't valid (but that doesn't matter for the question). What you can do is use the id in the name like this
<input type="checkbox" name="option[1]" class="validate[required]" value="yes" />Yes
<input type="checkbox" name="option[1]" class="validate[required]" value="no" />No
<input type="checkbox" name="option[2]" class="validate[required]" value="yes" />Yes
<input type="checkbox" name="option[2]" class="validate[required]" value="no" />No
Now you will know by the value if yes or no has been clicked and by the key the id. Also you might consider changing to radiobuttons, then one has to chose either one but can't answer both.

Radio button $_POST

How to know the value of clicked button
<input type="radio" name="radio">Yes<br>
<input type="radio" name="radio">No
php part
$option1= $_POST['radio'];
When i echo it it always say's "ON".
You need to give a value attribute:
<input type="radio" name="radio" value="yes">Yes<br>
<input type="radio" name="radio" value="no">No
Try this:
<input type="radio" name="radio" value="Yes_value">Yes<br>
<input type="radio" name="radio" value="No_value">No
and in PHP
$option1 = $_POST['radio']; // "Yes_value" or "No_value"
Try This:
Write its value in input
<input type="radio" name="radio" value="Yes" >Yes<br>
<input type="radio" name="radio" value="No">No
Use value attribute
<input type="radio" name="radio" value="Yes" />
<input type="radio" name="radio" value="No" />
assigned different value on the radio button.
<input type="radio" name="radio" value="Yes">Yes<br>
<input type="radio" name="radio" value="No">No
<form>
<input type="radio" name="radio" value="Yes">Yes
<input type="radio" name="radio" value="No">No <br>
<input type="radio" name="radio2" value="Yes">Yes
<input type="radio" name="radio2" value="No">No <br>
<input type="radio" name="radio3">No Value
<input type="submit" value="submit">
</form>
<?php
print_r($_GET);
I suggest you put this in a new file and just run it, it will help you get a deeper understanding of how the radio button work in forms.
Play around, see the difference that u get with different names, and different values.
As you can see, a radio element with no value can only be on or blank (blank wont even be set in PHP)

Retrieving the Value of a Radio Button using PHP

Code for RadioButton:
<h1 style="margin:0; margin-top:10px; padding:0; padding-left:25px; padding-bottom:10px; font-family:sans-serif;">
</h1>
<div style="background:#1794FF; color:#fafafa; padding:10px;">
<h3></h3>
<table>
<tr>
<td>
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" />
<label for="radio1" class="css-label">Neighbourhood Only</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" checked="checked"/><label for="radio2" class="css-label">Zipcode Only</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio3" class="css-checkbox" />
<label for="radio3" class="css-label">Near Zipcode</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio4" class="css-checkbox" />
<label for="radio1" class="css-label">City-Wide</label>
</td>
</tr>
</table>
</div>
<div style="background:#1794FF; color:#222; padding:10px;">
Php Code:
<?PHP
$selected_radio = $_POST['radiog_lite'];
print $selected_radio;
?>
After a form submission I arrive at the code above. However, it says the value is "on". Why isn't it printing the chosen radio button name?
May you can assign a value:
PHP Code
<form method="POST">
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" value="1" />
<label for="radio1" class="css-label">Neighbourhood Only</label><br>
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" checked="checked" value="2"/>
<label for="radio2" class="css-label">Zipcode Only</label><br>
<input type="radio" name="radiog_lite" id="radio3" class="css-checkbox" value="3" />
<label for="radio3" class="css-label">Near Zipcode</label><br>
<input type="radio" name="radiog_lite" id="radio4" class="css-checkbox" value="4" />
<label for="radio1" class="css-label">City-Wide</label><br>
<input type="submit" value="Submit">
</form>
PHP Code:
<?php
$selected_radio = $_POST['radiog_lite'];
print $selected_radio;
?>
A radio button is has a default value of "on", you should specify your value in your input initialization like so:
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" value="Your Value Here" checked="checked"/><label for="radio2" class="css-label">Zipcode Only</label>
So you can replace "Your Value Here" with "Zipcode Only" for this one. The label is just to describe the radio button value for the front-end user.
Use a value attribute on your input radio.
Use form method "post" and also put value for each radio:
<form method="post">
<table>
<tr>
<td>
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" value="Neighbourhood Only" />
<label for="radio1" class="css-label">Neighbourhood Only</label>
</td>
<tr>
</table>
<input type="submit" />
</form>

How to select check box on click the text using php?

In the image below, if I click either male or female text, I want the checkbox to be checked. How can I do that?
Second option is to wrap both checkbox and text in a label. It will work too:
<label>
<input id="male_checkbox" name="gender" type="checkbox" value="male" /> Male
</label>
You need to use label with for attribute:
<label for="male_checkbox">Male</label>
<input id="male_checkbox" name="gender" type="checkbox" value="male" />
<input type="checkbox" name="male" id="male" value="male"><label for="male">Male</label>
This will allow the user to click either the name or the checkbox
Do the same for female.

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 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>

Categories