Ross
Ross

Reputation: 61

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

Upvotes: 0

Views: 103

Answers (2)

Scott
Scott

Reputation: 21890

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

Upvotes: 1

user6830821
user6830821

Reputation:

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

Upvotes: 1

Related Questions