Forum Moderators: coopster

Message Too Old, No Replies

best way to deal with unselected radio buttons?

         

ocelot

4:06 am on Dec 2, 2004 (gmt 0)

10+ Year Member



I have a form with some radio buttons and text inputs. I want to add all the responses to a database table, but I want to allow users the option of not responding to certain questions on the form, including the radio button ones.

I find that when I submit a form with no radio button selected in a particular group, neither the radio button group's key nor its value (null) is sent to the script I'm submitting it to.

I was going to just say

if(!isset($_POST[$key]) ) {
$_POST[$key] = "No Response";
}

but that doesn't work when the $key isn't even set.

how can I deal with it?

Robber

9:34 am on Dec 2, 2004 (gmt 0)

10+ Year Member



Could you have a default option which starts of as the selected radio box, then at least you would always have something getting submitted.

ergophobe

9:56 am on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can have your array indexes the same as your column names. Then, you just have to run through the column names.

$radio_column_array = array("field4", "field5", "field6", "field9");

$update = "UPDATE mytable SET ";
$q = "SHOW COLUMNS FROM mytable";
$result = mysql_query($q);


foreach ($radio_column_array as $col_name)
{
$update .= (isset($_POST[$col_name]))
? $colname . '=`' . $_POST[$col_name] . '`,'
: $colname . '=`No Response`,'
}
substr($update, 0, -1); // to strip off the last comma
$update .= " WHERE id=$id";

Something like that might work for you.

Tom