PHP - not sure how to ask - regarding variables and $_POST
Posted
by Phil
on Stack Overflow
See other posts from Stack Overflow
or by Phil
Published on 2010-06-11T16:22:55Z
Indexed on
2010/06/11
16:32 UTC
Read the original article
Hit count: 190
php
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.
© Stack Overflow or respective owner