Radio Groups

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have a database which holds data to say On or Off, i have a form with a
radio group called status. I can change the status and submit it to the
database fine. But how do u get the radio group to tell me if it's on or
off? by checking the correct button?

Kathryn (Fire Juggler)

Re: Radio Groups

You need to set the value attribute of the radio buttons to "On" and
"Off" or 1 and 0 respectively like so:

<input type="radio" name="status" value="1"> On <br>
<input type="radio" name="status" value="0"> Off <br>

When the form is posted, the value of the form variable 'status' will
be set according to the selection, so you just use that.

Hope this helps!

Fire Juggler wrote:
Quoted text here. Click to load it

Re: Radio Groups

So for example:

$sql = "SELECT status FROM thequestion WHERE id = 42";
$res = mysql_query($sql);
if (!$res) {
    db_error("Could not select status", $sql);
$row = mysql_fetch_assoc($res);

Off <input type="radio" name="status" value="0" <?php if
(empty($row['status']))  echo 'CHECKED'; ?>> or
On  <input type="radio" name="status" value="1" <?php if
(!empty($row['status'])) echo 'CHECKED'; ?>>

Site Timeline