I have 2 following radio boxes in a form,
<input type="radio" name="radio" value="yes" class="radio" /> Yes
<input type="radio" name="radio" value="no" class="radio" /> No
How can i recieve the value of the radio button once the form is posted (in PHP)
Once it is posted on the same page, how can I remember the selected radio button and keep that checked?
Thanks.
1) The value of the radio button is saved in $_POST only if any of the choices was selected.
if (isset($_POST['radio'])) // if ANY of the options was checked
echo $_POST['radio']; // echo the choice
else
echo "nothing was selected.";
2) Just check for the value and add checked='checked' if it matches.
<input type="radio" name="radio" value="yes" class="radio" <?php if (isset($_POST['radio']) && $_POST['radio'] == 'yes'): ?>checked='checked'<?php endif; ?> /> Yes
<input type="radio" name="radio" value="no" class="radio" <?php if (isset($_POST['radio']) && $_POST['radio'] == 'no'): ?>checked='checked'<?php endif; ?> /> No
<input type="radio" name="radio" value="yes" class="radio" /> Yes
<input type="radio" name="radio" value="no" class="radio" /> No
u get radio value using $_POST['radio'];
simple bro,
<input type="radio" name="radio" <?php if($_POST['radio']=="yes") echo "checked";?> value="yes" class="radio" /> Yes
u have to identify radio box by value man
How can i recieve the value of the radio button once the form is posted (in PHP)
$_POST['radio']
Once it is posted on the same page, how can I remember the selected radio button and keep that checked?
Add a checked attribute if the value is equal to $_POST['radio'].
1) u will receive only that radio box value via POST which is checked
$radio_value=$_POST['radio'];
2)
<input type="radio" name="radio" value="yes" class="radio"
<?php echo ($radio_value == 'yes') ? 'checked="checked"' : ''; ?>/> Yes
<input type="radio" name="radio" value="no" class="radio"
<?php echo ($radio_value == 'no') ? 'checked="checked"' : ''; ?>/> No
Related
i have a radio button in my form
<input type="radio" id="radio-2" name="radio" checked onclick="calculate()" >
how to set the radio button value based on database value like
if(db_val==2){
value=50
} else{
value=100
}
Radio button value is depend on checked attribute
If you set the checked attribute with condition then its value will be set
Example:
<input type="radio" id="radio-2" name="radio" value="50" <?php echo ($db_val==2)?'checked':'' ?> onclick="calculate()" >
<input type="radio" id="radio-2" name="radio" value="100" <?php echo ($db_val!=2)?'checked':'' ?> onclick="calculate()" >
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>
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)
I am making a form and the radio button html is
<p>Response Required?
<label><input type="radio" name="required" vaule="yes" checked="checked">Yes</label>
<label><input type="radio" name="required" value="no" >No</label>
</p>
however when looking at the $_POST data after the form is submitted I get [required] => on instead of a "yes" or "no" any thoughts?
Try first to fix "vaule" to "value".
<p>Response Required?
<label><input type="radio" name="required" value="yes" checked="checked">Yes</label>
<label><input type="radio" name="required" value="no" >No</label>
</p>
value spelling is wrong at 1st radio button
I made a form with radio buttons. How can I preserve it's state after a user picked a choice? Then same form will show again in the next page and the radio button that the user picked is enabled.
//page1.html
<form method="post" action="page2.html">
<p>
<input type="radio" name="q1" value="A" />
A. <br />
<input type="radio" name="q1" value="B" />
B. <br />
<input type="radio" name="q1" value="C" />
C. <br />
<input type="radio" name="q1" value="D" />
D.
<p>
<input type="submit" name="action" value="Enter" />
</p>
</form>
To get the value of q1 on the next page, you would use $_POST['q1']. You can verify that the element has been posted, and the value matches the specific radio button by using if(isset($_POST['q1'])) && $_POST['q1'] == VALUE. So your form code would look like -
<input type="radio" name="q1" value="A" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'A')) echo 'checked="checked" ';?>/>
A. <br />
<input type="radio" name="q1" value="B" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'B')) echo 'checked="checked" ';?>/>
B. <br />
<input type="radio" name="q1" value="C" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'C')) echo 'checked="checked" ';?>/>
C. <br />
<input type="radio" name="q1" value="D" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'D')) echo 'checked="checked" ';?>/>