Forum Moderators: coopster

Message Too Old, No Replies

Radio Buttons and setting checked

         

AndyD

8:44 pm on Feb 18, 2004 (gmt 0)

10+ Year Member



I've got a form that updates a database with a value using a Radio Button, by default Beer is selected ensuring that the user MUST return a value

Lager Wine Beer Spirits

<input type="radio" name="category" value="Beer" CHECKED>Beer&nbsp;&nbsp;
<input type="radio" name="category" value="Lager">Lager&nbsp;&nbsp;
<input type="radio" name="category" value="Wine">Wine&nbsp;&nbsp;
<input type="radio" name="category" value="Spirits">Spirits&nbsp;&nbsp;

If I want the user to update this via another page and form, how do I set the default value? I can read in the value from the database, say WINE, but how do I set WINE as the default value for the user to modify, if required?

Do I set some IF statements?

if ($category == "Beer") {
<input type="radio" name="ud_category" value="Beer" CHECKED>Beer&nbsp;&nbsp;
} Else {
<input type="radio" name="ud_category" value="Beer">Beer&nbsp;&nbsp;
} ....etc....etc

Or is there a quick method?

Look forward to some answers.

Andy

Birdman

9:01 pm on Feb 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd probably throw the all the radio values into an array(), then loop through them.

$selections = array("Wine","Beer","Lager","Spirits"); //build array

for($i = 0; $i < count($selections); $i++) //start looping
{
if ($_POST["category"] == $selections[$i])
{
print "<input type=\"radio\" value="".$selections[$i]."" name=\"ud_category\" CHECKED>".$selections[$i]."&nbsp;&nbsp />;
} else {
print "<input type=\"radio\" value="".$selections[$i]."" name=\"ud_category\">".$selections[$i]."&nbsp;&nbsp />;
}
}

AndyD

9:19 pm on Feb 18, 2004 (gmt 0)

10+ Year Member



I must be doing something wrong.... Here is the code I've got.. so far...

<some code removed, but nothing that affects the form>

$category=mysql_result($result,$i,"category");
$priority=mysql_result($result,$i,"priority");

?>

<form onSubmit="name="frm" action="updated.php" method="post">

<input type="hidden" name="ud_id" value="<? echo "$id";?>">

$selections = array("Wine","Beer","Lager","Spirits"); //build array

for($i = 0; $i < count($selections); $i++) //start looping
{
if ($_POST["category"] == $selections[$i])
{
print "<input type=\"radio\" value="".$selections[$i]."" name=\"ud_category\" CHECKED>".$selections[$i]."&nbsp;&nbsp />;
} else {
print "<input type=\"radio\" value="".$selections[$i]."" name=\"ud_category\">".$selections[$i]."&nbsp;&nbsp />;
}
}

<br>

Priority: <input type="text" name="ud_priority" value="<? echo "$priority"?>"><br>

<input type="Submit">
<input type="reset" value="Reset!"><br>

</form>

Andy.

Birdman

9:33 pm on Feb 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just before the $selections =... line, you need to go back into PHP mode by using "<?php".

Right before the <br> line, you have to go back into HTML mode by using "?>".

AndyD

9:48 pm on Feb 18, 2004 (gmt 0)

10+ Year Member



Putting thoes commands in just give me a blank page. The script doesn't run any more?

Birdman

10:51 pm on Feb 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sorry. I have two errors in my example.

Try inserting this instead:

<form onSubmit="name="frm" action="updated.php" method="post">
<input type="hidden" name="ud_id" value="<? echo $id;?>">
$selections = array("Wine","Beer","Lager","Spirits"); //build array
for($i = 0; $i < count($selections); $i++) //start looping {
if ($_POST["category"] == $selections[$i]) {
print "<input type=\"radio\" value="".$selections[$i]."" name=\"ud_category\" CHECKED>".$selections[$i]."&nbsp;&nbsp />";
} else {
print "<input type=\"radio\" value="".$selections[$i]."" name=\"ud_category\">".$selections[$i]."&nbsp;&nbsp />";
}
}
<br>
Priority: <input type="text" name="ud_priority" value="<? echo $priority?>"><br>
<input type="Submit">
<input type="reset" value="Reset!"><br>
</form>

AndyD

11:55 pm on Feb 18, 2004 (gmt 0)

10+ Year Member



Birdman - After changing your code slightly, I've got the routine to work. I've replaced some of the " quotes with ' quotes and changed the IF statement. Probably due to me not explaining exactly what I was trying to do.

if ($category == $selections[$i]) {
print "<input type='radio' value='".$selections[$i]."' name='ud_category' CHECKED>".$selections[$i]."&nbsp;&nbsp ";
} else {
print "<input type='radio' value='".$selections[$i]."' name='ud_category'>".$selections[$i]."&nbsp;&nbsp ";
}

Once again, thanks for your help. This is a great forum for us novices. I'm please to see that help is given without making us look stupid when we ask the simple questions. Keep up the excellent work.

Birdman

4:05 am on Feb 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're welcome. Now I see the other error I made. As you can see, I escape the double quotes with \. If you look at the line below:

print "<input type=\"radio\" value="".$selections[$i].""

it should read:

print "<input type=\"radio\" value=\"".$selections[$i]."\"

I never escaped the quotes for the value attribute.