Checking Radiobuttons

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Greg B.
Forum Newbie
Posts: 3
Joined: Sat Dec 21, 2002 7:48 pm
Contact:

Checking Radiobuttons

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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 ;)
Greg B.
Forum Newbie
Posts: 3
Joined: Sat Dec 21, 2002 7:48 pm
Contact:

Post by Greg B. »

It's clean enough for me. Thanks a lot.
Post Reply