Retrieve mysql value from php for radio buttons - php

As trivial as it can seems, I'm having problems retrieving values for radio buttons from MySql database through PHP. It's my first learning project so I'm trying my best
Question has already been asked but I found no useful answer.
The php code does a simple "Select *" so I retrieve all the fields.
This is the php code
<label>Owner: <?php echo $row['Owner']; ?></label></br>
<input type="radio" name="Owner" checked=<?php if($row['Owner'] = "A") { echo "true"; }?> value="A">A
<input type="radio" name="Owner" checked=<?php if($row['Owner'] = "B") { echo "true"; }?> value="B">B</br></br>
and I retrieve the values with mysqli_fetch_array().
This is the result:
As you can see the label retrieves the correct value, the radio buttons not.
I've already tried putting == instead of = and putting ' instead of " but I don't know why the checkbox "B" is checked, since Owner value is A.
Also, if there are any best practices which are better than this, you're welcome.

The HTML attribute checked should not get a value, its mere presence indicates that the radio button is checked. So do this:
<label>Owner: <?php echo $row['Owner']; ?></label></br>
<input type="radio" name="Owner" <?php if($row['Owner']=="A") {echo "checked"}?> value="A">A
<input type="radio" name="Owner" <?php if($row['Owner']=="B") {echo "checked"}?> value="B">B
Or using the more compact short echo tag <?= .. ?> and ternary operator:
<label>Owner: <?=$row['Owner']?></label></br>
<input type="radio" name="Owner" <?=$row['Owner']=="A" ? "checked" : ""?> value="A">A
<input type="radio" name="Owner" <?=$row['Owner']=="B" ? "checked" : ""?> value="B">B
Note that you need double equals signs for comparisons.

try this code not = but use ==
<input type="radio" name="Owner" <?php if($row['Owner'] == "A") { echo "checked"; }?> value="A">

$gender=$row['gender'];
<input type="radio" name="gender" <?php if($gender=="Male"){?> checked="true" <?php } ?> />Male
<input type="radio" name="gender" <?php if($gender=="Female"){?> checked="true" <?php } ?>/>Female

$owner=$row['owner'];
$owners= ['A'=>'', 'B'=> ''];
$owners[$owner] = 'checked';
<input type="radio" name="Owner" <?php echo $owners['A']?> value="A">A
<input type="radio" name="Owner" <?php echo $owners['B']?> value="B">B
i think it's easy and simple and so useful if you have many values in DB.

i tried the answer given by #trincot but it give me errors so i have make little changes to improve the answer
<input type="radio" name="Owner" <?php if($row['Owner']=="A") {?> <?php echo "checked";?> <?php }?> value="A">A
<input type="radio" name="Owner" <?php if($row['Owner']=="B") {?> <?php echo "checked";?> <?php }?> value="B">B

Related

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";?> />

PHP my checking in the if statement to re-populate a checked button is not working

I recently found out how to re-populate checked radio buttons.. but Im not sure why right now its jumping to re-populate the last one of them even if the checked is the first one.
I can't find my mistake, I hope some can.. Thanks lots..
<tr>
<td class="right text">Title:</td>
<td><input type="radio" name="sex" value="Mr." <?php if (isset($_POST['sex']) == "Mr.") echo "checked"; ?>>Mr.</td></tr>
<tr><td></td><td><input type="radio" name="sex" value="Mrs." <?php if (isset($_POST['sex']) == "Mrs.") echo "checked"; ?>>Mrs.</td></tr>
<tr><td></td><td><input type="radio" name="sex" value="Ms." <?php if (isset($_POST['sex']) == "Ms.") echo "checked"; ?>>Ms.</td></tr>
change this for all three radio buttons.
<?php if ( isset($_POST['sex']) && $_POST['sex'] == "Mr.") echo "checked"; ?>
Your test is wrong. You're testing to see if the POST field "sex" is set. Then you're saying "Is this equivalent to this value?" So you're taking a boolean and comparing it to a string. So none of your tests will work.
The best way to do this is to move your test into an array. It means less tests and cleaner HTML
<?php
$data = array('Mr.' => null, 'Mrs.' => null, 'Ms.' => null);
if(isset($_POST['sex'])) $data[$_POST['sex']] = ' checked="checked"';
?>
<td><input type="radio" name="sex" value="Mr."<?php echo $data['Mr.'] ?> />Mr.</td></tr>
<tr><td></td><td><input type="radio" name="sex" value="Mrs."<?php echo $data['Mrs.'] ?> />Mrs.</td></tr>
<tr><td></td><td><input type="radio" name="sex" value="Ms."<?php echo $data['Ms.'] ?> />Ms.</td></tr>

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>

Why is PHP not allowing comparison?

I'm using PHP to read if an entry in my table on the database is set to "yes" or "no" and auto check the radio button that corresponds:
<?php include 'file.php';
$query = "SELECT * FROM TABLE";
$runquery = odbc_exec($connect,$query);
$status= odbc_result($runquery,"status");
odbc_close($file);
?>
<form>
<div class="formContainer">
<fieldset>
<legend>Campus Alert<span class="tooltip">Turn campus alert on and off.</span></legend>
<?php echo $status; ?>
Yes <input type="radio" name="alertStatus" id="alertStatus" value="yes" <?php if($status== "yes") echo "checked";?>>
No <input type="radio" name="alertStatus" id="alertStatus" value="no" <?php if($status== "no") echo "checked";?>>
</fieldset>
</div>
the <?php echo $status; ?> is for debugging so I can make sure what the database says and the form's reaction is correct. It prints "yes" (no quotes). However, the if statement will not respond. Any idea why it's doing this?
Have you tried changing your if statements to something like
<?php if(strtolower(trim($status)) == "yes") echo "checked";?>
It's not very good practise to use "yes/no" for your $status, you're better off using an int or boolean value.
Wow, that's really weird... the problem isn't with your PHP. First of all, You should remove the id attributes from those fields. But the real issue is that Firefox doesn't seem to want to check the second field when it has the name alertStatus. if you change the name to something else, it seems to be working. I'm not really sure why this is though.
Here's my test code:
<?php //include 'file.php';
//$query = "SELECT * FROM TABLE";
//$runquery = odbc_exec($connect,$query);
//$status= odbc_result($runquery,"status");
//odbc_close($file);
$status='no';
?>
<form>
<div class="formContainer">
<fieldset>
<legend>Campus Alert<span class="tooltip">Turn campus alert on and off.</span></legend>
<?php echo $status; ?>
Yes <input type="radio" name="alertStatu" value="yes" <?php if($status== "yes") echo "checked";?>>
No <input type="radio" name="alertStatu" value="no" <?php if($status== "no") echo "checked";?>>
</fieldset>
</div>
If you look at the page source you'll see that the check is actually there. However, you have a duplicate ID and the browser is getting confused. Replace:
Yes <input type="radio" name="alertStatus" id="alertStatus" ....>
No <input type="radio" name="alertStatus" id="alertStatus" .....>
with
Yes <input type="radio" name="alertStatus" id="alertStatus:yes" ....>
No <input type="radio" name="alertStatus" id="alertStatus:no" .....>
and it'll fix.

uncheck a checkbox when checking another

<?php
session_start();
if (count($_POST) > 0) {
$_SESSION['link'] = $_POST['link'];
}
?>
<form method="post">
Gmail: <input type="checkbox" name="link" value="gmail" id="gmail" <?php if ($_SESSION['link'] == 'gmail') echo "checked"; ?>>
Hotmail: <input type="checkbox" name="link" value="hotmail" id="hotmail" <?php if ($_SESSION['link'] == 'hotmail') echo "checked"; ?>>
<input type="submit" value="Spara">
</form>
Problem is if a box is checked you have to uncheck that then check another to change.
Is there a way so it unchecks the checked one when i check another? that sounds weird...
Thanks
You could just use Radio buttons instead, e.g:
<input type="radio" name="rdGroup1" value="John"> John
<input type="radio" name="rdGroup1" value="Jane"> Jane
There is a reason God created radio buttons. :)

Categories