print error if array is set to [0]
Posted: Wed Feb 11, 2009 2:04 pm
Hi
I'm very new at php and wondering if someone could point me in the right direction.
Our code retains the value of the array when submitted (if form has to be re-displayed with errors) however I'm having a hard time having it print an error if the field is not selected (in this case... $months still says 'Select Month' or 0)
$months = array ('Select Month','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
$sticky_months='';
if(isset($_POST['Submitted'])) {
$sticky_months = $_POST['month'];
}
echo '<select id="size-1" name="month">';
foreach ($months as $key => $value) {
if($key == $sticky_months) {
echo '<option value="' . $key .'" selected>' . $value . '</option>';
}else{
echo '<option value="' . $key .'">' . $value . '</option>';
}
}
echo '</select>';
//for loop for days - drop down menu
$sticky_day='';
if(isset($_POST['Submitted'])) {
$sticky_day = $_POST['day'];
}
echo '<select id="size-2" name="day">';
for ($day = 1; $day <=31; $day++) {
//echo "<option value=\"$day\">$day</option>";
if($day == $sticky_day) {
echo '<option value="' . $day .'" selected>' . $day . '</option>';
}else{
echo '<option value="' . $day .'">' . $day . '</option>';
}
}
echo '</select>';
if anyone has any ideas, I'd appreciate it
I have tried adding all sorts of isset and
if($months = ['0']) {
$errors[]= '<li class="error-list">Shirt Size</p>';
}
.. that error array works great for all my other fields...
and with the if($_POST['month'] && $months = ['0']... but got all kinds of neat errors...
Thank you in advance
Thank you
I'm very new at php and wondering if someone could point me in the right direction.
Our code retains the value of the array when submitted (if form has to be re-displayed with errors) however I'm having a hard time having it print an error if the field is not selected (in this case... $months still says 'Select Month' or 0)
$months = array ('Select Month','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
$sticky_months='';
if(isset($_POST['Submitted'])) {
$sticky_months = $_POST['month'];
}
echo '<select id="size-1" name="month">';
foreach ($months as $key => $value) {
if($key == $sticky_months) {
echo '<option value="' . $key .'" selected>' . $value . '</option>';
}else{
echo '<option value="' . $key .'">' . $value . '</option>';
}
}
echo '</select>';
//for loop for days - drop down menu
$sticky_day='';
if(isset($_POST['Submitted'])) {
$sticky_day = $_POST['day'];
}
echo '<select id="size-2" name="day">';
for ($day = 1; $day <=31; $day++) {
//echo "<option value=\"$day\">$day</option>";
if($day == $sticky_day) {
echo '<option value="' . $day .'" selected>' . $day . '</option>';
}else{
echo '<option value="' . $day .'">' . $day . '</option>';
}
}
echo '</select>';
if anyone has any ideas, I'd appreciate it
I have tried adding all sorts of isset and
if($months = ['0']) {
$errors[]= '<li class="error-list">Shirt Size</p>';
}
.. that error array works great for all my other fields...
and with the if($_POST['month'] && $months = ['0']... but got all kinds of neat errors...
Thank you in advance
Thank you