how to check a radio button

J

juicy

By default, I create several radio button and one of it checked="checked".
Now I would like to do some checking condition, and check the radio button
when condition match.

<?php if($c = 1){ ?>
<!-- check first radio button -->
<? else ?>
<!-- check second radio button -->
<? } ?>

Please give some idea or useful links. Thanks.
 
M

Mark Parnell

Deciding to do something for the good of humanity, juicy
<?php if($c = 1){ ?>

Please give some idea or useful links. Thanks.

<?php if($c == 1) { ?>
<input type="radio" checked="checked" value="1" name="c">
<input type="radio" value="2" name="c">
<? } else { ?>
<input type="radio" value="1" name="c">
<input type="radio" checked="checked" value="2" name="c">
<? } ?>

Perhaps not the most elegant solution, but it works. Gets messier the
more radio buttons you have. If it's only 2 like your example, then the
above will suffice.
 
B

Bob Long

In
Mark Parnell said:
Deciding to do something for the good of humanity, juicy


<?php if($c == 1) { ?>
<input type="radio" checked="checked" value="1" name="c">
<input type="radio" value="2" name="c">
<? } else { ?>
<input type="radio" value="1" name="c">
<input type="radio" checked="checked" value="2" name="c">
<? } ?>

Is it 'checked="checked"'? Or simply 'checked'? As in:

<input type="radio" checked value="2" name="c">
 
M

Mark Parnell

Deciding to do something for the good of humanity, Bob Long
Is it 'checked="checked"'? Or simply 'checked'?

Either is valid in HTML; only the first is in XHTML. IIRC some browsers
don't cope well with the short version either.
 
Joined
Jun 27, 2011
Messages
1
Reaction score
0
Hi juicy,

you can add the word checked by the condition, like this...

<input type="radio" name="color" value="1"
<? if($c==1) echo 'CHECKED';?> >

but careful because Checkboxes are off by default, but there is no default for which radio button is on when the form loads; most browsers have none on, some put the first one on.

Hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top