I use this code to validate select box value. But my problem is when I submit form without selecting an option in select box its not going to error message.
if ( !isset( $_POST['how_hear'])) {
$errors[] = 'Select an option from How did you hear us select box';
} else {
$howHear = $_POST['how_hear'];
}
this is my select box
$howHear = array( 1 => 'option value 01',
'option value 02',
'option value 03',
'option value 04',
'option value 05',
'option value 06',
'option value 07',
'option value 08'
);
echo "<select name='how_hear'>
<option>--Select an Option--</option>\n";
foreach ( $howHear AS $key => $value) {
echo "<option value='{$key}'";
// Check for stickyness:
if ( $_POST['how_hear'] == $key){
echo " selected='selected'";
}
echo ">$value</option>\n";
}
echo "</select>\n";
Can anybody tell my where I have gone wrongly?
Thank you.
The problem seems to be that how_hear is set. The default is
Check that
Alternatively, update your select field so that the default value is 0 (this would be the correct thing to do). And then check that the value is non-zero.