On Jun 24, 2011, at 1:44 PM, Chris Stinemetz wrote:
#### radio select validation ####
What I am doing wrong?
I want to make sure a radio button is selected, but my current code
allows insertion even when radio button isn't selected.
At the risk of repeating myself:
From: Tamara Temple <tamouse.lists@xxxxxxxxx>
Date: June 23, 2011 7:41:12 PM CDT
To: php-db list <php-db@xxxxxxxxxxxxx>
Subject: Re: [PHP-DB] radio form submission
You can also avoid the problem with an empty return if no radio
button is checked by making sure one is checked when your form
loads, by using the 'checked' attribute on one of the items. If you
want them to have a "not tested" option, you can include that as one
of your buttons and add it to the array above as well. (That might
be a good place to use the zero value.)
My code is:
//Generating radio buttons for store type with array
echo 'Store type:<br /><br />';
$choices = array('corporate' => 'Cricket owned | ',
'premier' => 'Premier dealer');
$firstchecked=FALSE;
foreach ($choices as $key => $choice) {
if (!$firstchecked) {
echo "<input type='radio' name='store_type' value='$key'
checked /> $choice".PHP_EOL;
$firstchecked = TRUE;
} else {
echo "<input type='radio' name='store_type' value='$key'/>
$choice \n";
}
}
//Validate the radio button submission
if (!array_key_exists($_POST['store_type'], $choices)) {
echo "You must select a valid choice.";
}
That's rather brute-force; perhaps some maven can come up with a
better scheme.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php