tags:

views:

103

answers:

4

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?

+1  A: 

If I understand correctly, wouldn't this meet your needs?

if ($_POST['product'] != 1)
dusk
+1  A: 

Consider setting the value of any "unselectable" or "un-filled-in" items to be '' (i.e., the empty string). That way they'll look to PHP the same as a text input that didn't get filled in.

Your !empty($_POST['phone']) setup would then work fine.

VoteyDisciple
+1  A: 

The easy way is make sure that that the value is not blank. When they select something, the value associated with that option is what is returned. The better way is to make sure that the value posted is within the allowed options (i.e. 1 <= x <= 6) because people can create false forms and post to your script with a value of, say, 7 (which is not blank).

So something like this:

if ($_POST[$product[1]] >= 1 && $_POST[$product[1]] <= 6) {
    // It's in range, do something
    // Make sure that $product[1] is defined
}
animuson
+1  A: 

How do I tell the dropdown box to show, for example, item 5?

$products = array(
 '' => 1,
 'Item 2' => 2,
 'Item 3' => 3,
 'Item 4' => 4,
 'Item 5' => 5,
 'Item 6' => 6
);

    function generateSelect($name = '', $options = array()) {
     $html = '<select name="'.$name.'">';
     foreach ($options as $option => $value) {
      $html .= '<option value='.$value.'>'.$option.'</option>';
     }
     $html .= '</select>';
     return $html;
    }

For a start, your $products array() is an odd way around, I'd do it like this:

$products = array(
 '',
 'Item 2',
 'Item 3',
 'Item 4',
 'Item 5',
 'Item 6', // last comma optional, but I tend to put it in when I'm writing a vertical list
);

And then the generateSelect() function should take a value as well as the array.

function generateSelect($name='', $options=array(), $currValue='') {
 $html = '<select name="' . $name . '">';
 foreach ($options as $k => $v) {
  $html .= '<option value="' . $k . '"' . ($k == $currValue ? ' selected="selected"' : '') . '>' . $v . '</option>';
 }
 $html .= '</select>';
 return $html;
}

Now if you a value has been selected, it will be kept, if you pass it to the generateSelect() function as $currValue. I think that was what you were asking.

TRiG
i am trying your suggestion now but $currValue is not being set
Phil
how do I pass it to generateSelect()? and do i pass it when the user submits?
Phil
I don't have enough context of your code to tell you how to integrate these functions. As far as I understand it, you're displaying a form and then, on submit, you are displaying the form again with the submissions filled in. Yes? In that case, on submission, pass the received value (from the `$_POST` array, yes?) to the `generateSelect()` function as `$currValue`.
TRiG