Keep radio button selected after a form submit - php

I am using below code to keep the radio button selection after a form submission, but it keep resetting to the last button after form submission
<input type="radio" name="button" value="Yes" <?php if(isset($_POST['button']) == 'Yes') echo ' checked="checked"';?> />Yes
<input type="radio" name="button" value="No" <?php if(isset($_POST['button']) == 'No') echo ' checked="checked"';?> />No
How can I keep the selection ?

isset() returns a boolean. As such, comparing directly to Yes/No is not what you want.
What you want is:
if (isset($_POST['button']) && $_POST['button'] == 'Yes')

Related

How do I add two php conditions inside an input type radio button?

My problem right now is that I have two conditions inside my radio buttons. Here's my current code. They work when they're used individually, but when they're together nothing happens.
<input type="radio" name="jsEnable" value="true" id="activatejs"
onchange="enable_link();" onclick="checkBoxEnabling()" <?php if
(isset($_POST['jsEnable']) && $_POST['jsEnable']==="true") {
$_POST['jsEnable'] = TRUE echo "checked='checked'";}?> />
<input type="radio" name="jsEnable" value="false"
id="donotactivatejs" onchange="disable_link();"
onclick="checkBoxDisabling()" <?php if (isset($_POST['jsEnable']) &&
$_POST['jsEnable']==="false") { $_POST['jsEnable'] = FALSE; echo
"checked='checked'"; }?> />
Can someone help? Anything wrong with my code? I am trying to convert the selected button parameter into boolean and then trying to retain the chosen radio button when a user submits and the form has an error.
if i understand you want to check your checkboxes if the value of $_POST['jsEnable'] is equal to the value of that checkbox, right?
<input type="radio" name="jsEnable" value="true" id="activatejs"
onchange="enable_link();" onclick="checkBoxEnabling()" <?php if
(isset($_POST['jsEnable']) && $_POST['jsEnable']==="true") { echo "checked='checked'"; $_POST['jsEnable'] = true }?> />
<input type="radio" name="jsEnable" value="false"
id="donotactivatejs" onchange="disable_link();"
onclick="checkBoxDisabling()" <?php if
(isset($_POST['jsEnable']) && $_POST['jsEnable']==="false") { echo "checked='checked'"; $_POST['jsEnable'] = false }?> />

Getting the value of a radio button

When I add a new record to my database the value of the radio button is inserted successfully. However, when I go to edit the record on my edit.php page, the radio buttons are all unchecked.
How I ensure that the corresponding radio button is checked depending on what I selected.
I've tried many different things and none work. This is the latest attempt:
<?php $prohibition = $row['prohibition']; ?>
Yes: <input type="radio" name="prohibition" value="Yes" <?php if($row['prohibition'] == "Yes") print "checked";?> /><br>
No: <input type="radio" name="prohibition" value="No" <?php if($row['prohibition'] == "No") print "checked";?> />
Based on the small snippet of code...
<?php if ($prohibition == 'Yes') { echo 'checked'; } ?> should work.
You've already defined the $prohibition variable so just check against that There's no reason to use the database output directly for the if statements.
Of course it depends entirely on the output of $row['prohibition']
What is the output of $row['prohibition']? try to replace print by echo :
<?php $prohibition = $row['prohibition']; ?>
Yes: <input type="radio" name="prohibition" value="Yes" <?php if($row['prohibition'] == "Yes") echo "checked";?> /><br>
No: <input type="radio" name="prohibition" value="No" <?php if($row['prohibition'] == "No") echo "checked";?> />

Using the radio button 'checked' value in HTML and PHP

So, I have a questions regarding the input type radio in HTML. As you know, you can put checked as a value, this will mark it as checked.
The story is I am getting a 0 or 1 value from my database. I am then checking if it's 0 or 1 and will then mark one of the radio button's as checked.
My code is as follows:
<?php if($pay_op == 0) { ?>
<input type="radio" value="paypal" id="pay_op" checked>PayPal<br />
<input type="radio" value="other" id="other_op">Other<br/>
<input type="submit" id="pay_op_submit" />
<?php } elseif ($pay_op == 1) { ?>
<input type="radio" value="paypal" id="pay_op">PayPal<br />
<input type="radio" value="other" id="other_op" checked>Other<br/>
<input type="submit" id="pay_op_submit" />
<?php } ?>
My problem now is, whenever I try to mark the other radio button as checked by clicking on it, both radio buttons are checked?
I thought that this might have something to do with me checking if the value returned from the database is 0 or 1 and it will keep one of the radio buttons checked until that value is changed. Now my question is, does anyone know a solution to this issue so that whenever someone clicks on something different than the default checked radio button it will actually check that one and not both of them?
Any tips are highly appreciated! =)
Thanks!
Radio buttons work basically as a named group. The browser only un-checks a radio button if it is linked to the other radio buttons with a property called name.
<?php
if($pay_op == 0)
{ ?>
<input name ="myGroup" type="radio" value="paypal" id="pay_op" checked>PayPal<br />
<input name ="myGroup" type="radio" value="other" id="other_op">Other<br/>
<input name ="myGroup" type="submit" id="pay_op_submit" />
<?php
}
elseif($pay_op == 1)
{ ?>
<input name ="myGroup" type="radio" value="paypal" id="pay_op">PayPal<br />
<input name ="myGroup" type="radio" value="other" id="other_op" checked>Other<br/>
<input name ="myGroup" type="submit" id="pay_op_submit" />
<?php
}
?>

Checking which radio button is selected in PHP

I have an HTML page that contains two radio buttons ("yes" and "no") along with a submit button.
How can I specify that I want to execute a PostgreSQL query ONLY if the submit button is pressed AND the "yes" radio button is selected?
Your radio buttons need to have the same name, so check the value that is submitted for that field:
if ($_POST['radio'] == 'Yes')
{
// Execute query
}
Given HTML like this
<form action="foo.php" method="post">
<label for="yesno_yes">
<input type="radio" name="yesno" value="yes" id="yesno_yes"> Yes
</label>
<label for="yesno_no">
<input type="radio" name="yesno" value="no" id="yesno_no"> No
</label>
<input type="submit" name="submit_btn" value="Submit">
</form>
You can check using this
<?php
if (isset($_POST['submit_btn'], $_POST['yesno']) && $_POST['yesno'] == 'yes') {
// do stuff here
}

Radio Button Array - Need Help

I have a radio button array that I need help with. Here is the code:
<input type="radio" name="radio" id="academic" value="1"<?php
if ($row_EventInfo['academic'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['academic'] = '';}
?>>
<label for="academic">Academic</label><br />
<input type="radio" name="radio" id="personal" value="1"<?php
if ($row_EventInfo['personal'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['personal'] = '';}
?>>
<label for="personal">Personal</label><br />
<input type="radio" name="radio" id="diversity" value="1"<?php
if ($row_EventInfo['diversity'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['diversity'] = '';}
?>>
<label for="diversity">Diversity</label><br />
What I'm trying to do is this. I have a column in my database table for each radio button because we have to have their inputs separately. However, I want them to only be able to select one of the buttons at a time. I changed all the names to be the same ("radio"), but since PHP MYSQL uses the names to know where to post the information in the table it doesn't know where to post.
Is there any way to create an if statement to tell it to only allow one button at a time to be selected and keep the inputs separate for the database table?
Please let me know if you need clarification. Thanks!
set the value to the column name, eg
<input type="radio" name="radio" id="diversity" value="diversity"
on the php end, simply do something like
$sql = "UPDATE table SET {$_POST['radio']} = 1";
this is in a form, right?
Then you get the data in the POST. I wouldn't set the value to 1, set the value to the value you want to select, e.g.
Then in the post, get the value and use this to set the data in the DB.
Hope this helps!
BR,
TJ
If you use:
<input type="radio" name="radio" id="diversity" value="value1">
<input type="radio" name="radio" id="diversity" value="value2">
<input type="radio" name="radio" id="diversity" value="value3">
<input type="radio" name="radio" id="diversity" value="value4">
it will be available in $_POST['radio']; with what ever radio button you selected. eg.value3 dont confuse yourself by naming input types with the same name
If you use:
<input type="radio" name="myradio" id="diversity" value="value1">
It will be available in $_POST['myradio'];
and so on
plus you may want to look into using the ternary operator
if ($row_EventInfo['diversity'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['diversity'] = '';}
into
echo ($row_EventInfo['diversity'] == '1') ? ' checked="checked"':'';

Categories