View Full Version : display radio button value in a form

01-19-2007, 08:45 AM
hey people,

so i am building an update form, to update a db record, and this form includes radio buttons.

in this case they represent whether a field in the db table is true or false (boolean data type).

So i have two radio buttons:

<input type="radio" name="answer" value="true"> Yes
<input type="radio" name="answer" value="false"> No

I can retrive the correct db row based on my sql statment but im not sure how to show which of the radio buttons should be checked based on the value from the db?

could anyone answer this for me?


01-19-2007, 09:28 AM
Where $value = the value variable from the db


<input type="radio" name="answer" value="true" <?php if(!(strcmp($value, 'true'))){echo "checked";} ?>>

<input type="radio" name="answer" value="false" <?php if(!(strcmp($value, 'false'))){echo "checked";} ?>>

01-19-2007, 09:47 AM
$answer = $row_user['answer'];
if ($answer == 't') {
$yes = 'checked="checked"';
} else {
$no == 'checked="checked"';
<input type="radio" name="answer" value="true"<?php echo $yes; ?> /> Yes
<input type="radio" name="answer" value="false" <?php echo $no; ?> /> No

(oh and im testing against t because im using a postgresql db, incase anyone was wondering)

cheers davidj

01-19-2007, 10:13 AM
i dont think you need..


I think ...


is allowed

also you have an equality operator (==) in your IF example mind.. Just a heads up

01-19-2007, 10:42 AM
yeah right, yuor appraoch is much cleaner and better, im using your approach,

cheers davidj

p.s. any chance of you doing a tutorial on how to create an update form for many to many relations, like when you assign staff to a project for instance!!! would be wicked!

01-19-2007, 01:35 PM
and simply
is the same thing in HTML, altough one is cleaner and shorter than the other ;)

01-19-2007, 01:44 PM
but which is right and which is wrong?

01-19-2007, 01:50 PM
They're both right. One is a shortform of the other.