Page 1 of 1

Checking Radiobuttons

Posted: Sat Jun 05, 2004 10:08 pm
by Greg B.
I have a noob question that I can't get past. I'm working on a little edit/update script and have run into a problem prechecking radiobuttons from data I queried from the database. Here is the part of the script where I am having trouble.

Code: Select all

<?php
echo ("
    Yes: <input type="radio" ". if ($row["a1"]=='1') {echo('checked');} ." name="a1" value="1"> No: <input type="radio" ". if ($row["a1"]=='1'||$row["a1"]=='') {echo('checked');} ." name="a1" value="0">
"); 
?>
What I'm trying to do is if the value from the database = 1, then it will check yes. But, if the value = 0 or nothing, it will check no. I've tried the script above but it returns parse error on that line. If anyone could clear up this problem, I'd appreciate it.

Posted: Sat Jun 05, 2004 10:18 pm
by markl999
Try:

Code: Select all

echo 'Yes :<input type="radio"';
if($row['al'] == 1) echo '  checked';
echo ' name="al" value="1"> No: <input type="radio"';
if($row['al'] == 0) echo ' checked';
echo ' name="al" value="0">';
There's probably a 'cleaner' way to do it (in fact i'm sure there is) but my brain can't think of it at 4:20am ;)

Posted: Sat Jun 05, 2004 10:40 pm
by Greg B.
It's clean enough for me. Thanks a lot.