how to check a radio button

Discussion in 'HTML' started by juicy, Mar 27, 2006.

  1. juicy

    juicy Guest

    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.
    juicy, Mar 27, 2006
    #1
    1. Advertising

  2. juicy

    Mark Parnell Guest

    Deciding to do something for the good of humanity, juicy
    <> declared in alt.html:

    > <?php if($c = 1){ ?>


    Will always return true. I think you mean <?php if($c == 1){ ?>.
    http://www.php.net/manual/en/language.operators.comparison.php

    > 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.

    --
    Mark Parnell
    Thankyou blinkynet!
    http://blinkynet.net/comp/uip5.html
    Mark Parnell, Mar 27, 2006
    #2
    1. Advertising

  3. juicy

    Bob Long Guest

    In news:,
    Mark Parnell <> typed:
    > Deciding to do something for the good of humanity, juicy
    > <> declared in alt.html:
    >
    > > <?php if($c = 1){ ?>

    >
    > Will always return true. I think you mean <?php if($c == 1){ ?>.
    > http://www.php.net/manual/en/language.operators.comparison.php
    >
    > > 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">
    > <? } ?>


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

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

    --
    Bob Long
    Bob Long, Mar 27, 2006
    #3
  4. juicy

    Mark Parnell Guest

    Deciding to do something for the good of humanity, Bob Long
    <> declared in alt.html:

    > 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.

    --
    Mark Parnell
    Thankyou blinkynet!
    http://blinkynet.net/comp/uip5.html
    Mark Parnell, Mar 27, 2006
    #4
  5. juicy

    yomna_fahmy

    Joined:
    Jun 27, 2011
    Messages:
    1
    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
    yomna_fahmy, Jun 27, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Grey
    Replies:
    1
    Views:
    2,228
    avnrao
    May 31, 2004
  2. Flip
    Replies:
    3
    Views:
    7,152
  3. =?Utf-8?B?SklNLkgu?=

    Q: check radio button

    =?Utf-8?B?SklNLkgu?=, May 16, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    594
    Yunus Emre ALPĂ–ZEN [MCAD.NET]
    May 17, 2005
  4. Scott D Johnson
    Replies:
    1
    Views:
    1,048
    Scott D Johnson
    Sep 27, 2006
  5. Hiten
    Replies:
    1
    Views:
    609
    Vidar Petursson
    May 26, 2004
Loading...

Share This Page