I have a PHP form. The form works but I’m trying to test to see if a value other than the first item has been selected. I can’t figure out how to write the If statement.
$products = array(
'' => 1,
'Item 2' => 2,
'Item 3' => 3,
'Item 4' => 4,
'Item 5' => 5,
'Item 6' => 6
);
$html = generateSelect('products', $products);
function generateSelect($name = '', $options = array()) {
$html = '<select name="'.$name.'">';
foreach ($options as $option => $value) {
$html .= '<option value='.$value.'>'.$option.'</option>';
}
$html .= '</select>';
return $html;
}
In my table, the drop down box is displayed:
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:center; padding:{$cell_padding}; font-weight:bold; {$product[3]}">{$product[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><select name="{$product[1]}">
<option value="1"></option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
<option value="4">Item 4</option>
<option value="5">Item 5</option>
<option value="6">Item 6</option>
</select></td>
</tr>
I use the following if statement to check to see if someone has entered a phone number. if they have not entered a phone number, then the “Phone:” text turns red. How do I do an if statement similar to this to verify that someone has selected a product option from the drop down box?
if(!empty($_POST['phone'])) {
$phone[2] = clean_var($_POST['phone']);
if (function_exists('htmlspecialchars')) $phone[2] = htmlspecialchars($phone[2], ENT_QUOTES);
}
else {
$error = 1;
$phone[3] = 'color:#d20128;';
}
it seems simple but I can’t figure it out.
EDIT 1:
I tried the suggestions, and the following works best so far:
if ($_POST['product'] != 1){
after I click the ‘submit’ button. the “Product:” text does turn red like expected; however, the drop down box resets to showing the option value 1 but the variable still remains at the selected value.
long questions short. how do I tell the dropdown box to show, for example, item 5?
If I understand correctly, wouldn’t this meet your needs?