4

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:

enter image description here

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.

1
  • You are clear, but maybe I'm not. I clearly stated: "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." Commented Nov 7, 2015 at 9:01

5 Answers 5

12

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.

Sign up to request clarification or add additional context in comments.

Comments

4

try this code not = but use ==

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

3 Comments

@LiquidCore - He's also using "checked" instead of "true" to mark what's checked.
Yeah, it ended up that I didn't knew how to use checked properly. I marked as answer who came first, since that guy answer was correct too.
<input type="radio" name="Owner" value="A" checked >
0
$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

1 Comment

This is a code-only answer (which should be avoided as much as possible on SO because it does a poor job of educating future readers). The radio inputs do not have value attributes as the OP's snippet uses. If you are going to bother writing a new answer to a question that already has an educational and working solution, you need to bring new information or a new technique that is valuable to the page ...otherwise, it is just page bloat that SO and its readers can do without.
0
$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.

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.