Checkstate of Radiobutton

The last two hours i've been trying to make my form work,
its contains two unchecked radiobuttons and a submit button.
The user is able to choose either one of the two
and send the form using the submit button.
I would like to check (checked="checked") the radiobutton the user
pickt _after_ submitting the form, but just can't get it to work.

So what is the secret?

Regards,
Samuel van Laere
Samuel van Laere [ Sa, 14 April 2007 21:57 ] [ ID #1687754 ]

Re: Checkstate of Radiobutton

Samuel van Laere wrote:
> The last two hours i've been trying to make my form work,
> its contains two unchecked radiobuttons and a submit button.
> The user is able to choose either one of the two
> and send the form using the submit button.
> I would like to check (checked="checked") the radiobutton the user
> pickt _after_ submitting the form, but just can't get it to work.

<form action="apage.php">
<input type="radio" name="group1" value="Milk"> Milk<br>
<input type="radio" name="group1" value="Butter"> Butter<br>
<input type="Submit">
</form>

switch($_REQUEST['group1']) {
case 'Milk':
echo "You like milk!<br>\n";
break;
case 'Butter':
echo "You like butter!!!<br>\n";
break;
default:
echo "You don't like anything<br>\n";
break; //not needed, but a good habbit
}

--

//Aho
Shion [ Sa, 14 April 2007 22:39 ] [ ID #1687755 ]

Re: Checkstate of Radiobutton

Message-ID: <46213219$0$79139$e4fe514c [at] news.xs4all.nl> from Samuel van
Laere contained the following:

>I would like to check (checked="checked") the radiobutton the user
>pickt _after_ submitting the form, but just can't get it to work.
>
>So what is the secret?

A little function

function checkit($name_of_radiobutton,$val){
$checked=(isset($POST[$name_of_radiobutton])
&&$POST[$name_of_radiobutton]==$val)?"checked=\"checked\"" :"";
return $checked;
}

then in the code

<input type='radio' name='rb' value='foo' <?php echo
checkit('rb','foo);?>>
<input type='radio' name='rb' value='bar' <?php echo
checkit('rb','bar);?>>

Untested.
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Geoff Berrow [ Sa, 14 April 2007 22:42 ] [ ID #1687756 ]

Re: Checkstate of Radiobutton

"Geoff Berrow" <blthecat [at] ckdog.co.uk> schreef in bericht
news:qle223tb8f32u454acoa2j8nc9cegg9luc [at] 4ax.com...
> Message-ID: <46213219$0$79139$e4fe514c [at] news.xs4all.nl> from Samuel van
> Laere contained the following:
>
>>I would like to check (checked="checked") the radiobutton the user
>>pickt _after_ submitting the form, but just can't get it to work.
>>
>>So what is the secret?
>
> A little function
>
> function checkit($name_of_radiobutton,$val){
> $checked=(isset($POST[$name_of_radiobutton])
> &&$POST[$name_of_radiobutton]==$val)?"checked=\"checked\"" :"";
> return $checked;
> }
>
> then in the code
>
> <input type='radio' name='rb' value='foo' <?php echo
> checkit('rb','foo);?>>
> <input type='radio' name='rb' value='bar' <?php echo
> checkit('rb','bar);?>>
>
> Untested.

Thank you Geoff, thats just what I need!
Not bad for untested code though :)

Cheers,
Sam
Samuel van Laere [ So, 15 April 2007 00:32 ] [ ID #1688248 ]
PHP » alt.php » Checkstate of Radiobutton

Vorheriges Thema: date(W) difficulty
Nächstes Thema: PHP and MySql getting the month name