PDA

View Full Version : display radio button value in a form


lux
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?

thanks

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

example...



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

lux
01-19-2007, 09:47 AM
<?php
$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

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

'checked="checked"';

I think ...

"checked"

is allowed

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

lux
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!

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

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

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