display radio button value from sql on page load - php

Making a form to edit sql table, form uses radio button and so user can see the existing value, when page loads, I would like my radio buttons to be indicated on/off according to the db. There are 2 buttons, they send values 0 & 1. I'm currently using isset with no success. Is there a better (correct) way to do this? My php is working for the text fields, but with the radio nothing is selected.
<label>
<input type="radio" name="transaction" value="0" <?php if (isset($transaction) && $transaction === '0' ) { echo 'checked="checked"';}?>/>
Listings</label>
<br />
<label>
<input type="radio" name="transaction" value="1" <?php if (isset($transaction) && $transaction === '1' ) { echo 'checked="checked"';}?>/>
Transaction History</label>

Most likely the value of $transaction is a number, not a string. Change the comparison to check against a number, not against a string:
<input type="radio" name="transaction" value="0" <?php if (isset($transaction) && $transaction === 0 ) { echo 'checked';}?>/>
<input type="radio" name="transaction" value="1" <?php if (isset($transaction) && $transaction === 1 ) { echo 'checked';}?>/>
Alternatively, use the weak equality == operator to just compare the values, not the types:
<input type="radio" name="transaction" value="0" <?php if (isset($transaction) && $transaction == '0' ) { echo 'checked';}?>/>
<input type="radio" name="transaction" value="1" <?php if (isset($transaction) && $transaction == '1' ) { echo 'checked';}?>/>

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 }?> />

Keep radio button selected after a form submit

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

Checked radio that is insert in database when select

Assuming this that we in time insert data in database has 3 input:radio and with select one from they, insert value it to database. now how in select from database same radio that inserted in database, is checked.
Example:
We have 3 input:radio as:
<input type="radio" name="type1" value="value1">
<input type="radio" name="type2" value="value2" checked>
<input type="radio" name="type3" value="value3">
With checked value2 inserted it in database, now we want show(select) all radios and checked it radio that is inserted to database.as(this is after select from database):
<input type="radio" name="type1" value="value1">
<input type="radio" name="type2" value="value2" checked> // this value was in the database
<input type="radio" name="type3" value="value3">
How can fix it with PHP?
You should, as alreay said, use the same name for the radio buttons. Also, the right sintax is checked="checked" :
<input type="radio" name="type" value="value1">Value 1
<input type="radio" name="type" value="value2" checked="checked">Value 2
<input type="radio" name="type" value="value3">Value 3
When retrieved from database, you can check if value equals to the one in your html, and set a checked attribute accordingly.
<input type="radio" name="type" value="value1" <?php echo ($query_hs->type == 'value1') ? 'checked="checked"' :'';?>>Value 1
<input type="radio" name="type" value="value2" <?php echo ($query_hs->type == 'value2') ? 'checked="checked"' :'';?>>Value 2
<input type="radio" name="type" value="value3" <?php echo ($query_hs->type == 'value3') ? 'checked="checked"' :'';?>>Value 3
Just substitute your own values with my standard one, like:
<input type="radio" name="type" value="hotel" <?php echo ($query_hs->type == 'hotel') ? 'checked="checked"' :'';?>>Hotel
First of all, name of the radio element should be same for all three radio buttons so that they form a group. Otherwise, your users will be able to select all three.
Second, define the set of values as array in PHP. And run a forloop to generate HTML code. Something on these lines -
<?php
$radio_values = array("value1", "value2", "value3"); // Assuming your set of values is static
foreach($radio_values as $value) {
$value_from_db = read_radio_value(); // Replace this with your logic to fetch values from database
if ($value_from_db == $value) $checked = "checked";
else $checked = "";
echo "<input type=\"radio\" name=\"type\" value=\"{$value[$i]}\" {$checked}>";
}
?>

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"':'';

keeping radio button value after post

HI
i'm using a php page and i need to keep the value of and check box and radio button (checked or not checked) after post page.
how could i make it?
thanks
First get the radio button value.
$radiobuttonvalue = $_POST['radiobuttoname']
Then for each radio button with the same name, do this
<input type="radio" name="radiobuttonname" value="value" id="radiobuttonname" <?php if($radiobuttonvalue == "value") { echo 'checked="checked"';} ?>
You need something like:-
<?php
$postCheckboxName = '';
if (isset($_POST['checkbox_name']) || 'any_value' == $_POST['checkbox_name']) {
$postCheckboxName = ' checked="checked"';
}
?>
<input type="checkbox" name="checkbox_name" value="any_value"<?php echo $postCheckboxName;?> />
<?php
$postRadioName = '';
if (isset($_POST['radio_name']) || 'any_other_value' == $_POST['radio_name']) {
$postRadioName = ' checked="checked"';
}
?>
<input type="checkbox" name="radio_name" value="any_other_value"<?php echo $postRadioName;?> />
This code should get you going. I'm basically checking whether the POST value of either the checkbox / radio element is set or not & whether the corresponding element's value matches with my respective element's value or not.
Hope it helps.
Something like this:
<?php if (isset($_POST['checkbox_name']))?>
<input type="checkbox" checked="checked" value="<?php echo $_POST['checkbox_name'];?>" />
<?php} ?>
<?php if (isset($_POST['radio_name']))?>
<input type="radio" checked="checked" value="<?php echo $_POST['radio_name'];?>" />
<?php} ?>
What happens is that you check if the input variables are in the $_POST and if so you add checked="checked" to the input fields to make them checked.
This worked for me, and is self explanatory
sample code usage:
<div class="form-group">
<label class="radio-inline">
<input type="radio" name="time" value="lunch" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='lunch' ){echo ' checked="checked"';}?>>Lunch</label>
<label class="radio-inline">
<input type="radio" name="time" value="dinner" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='dinner' ){echo ' checked="checked"';}?>>Dinner</label>
</div>

Categories