0

How can I use PHP to raise an error if this particular value is selected?

<option>Please Select</option>

3
  • 1
    Please explain what you are trying to accomplish. What do you mean by "raise an error"? Commented Aug 8, 2012 at 2:24
  • Just like any $_POST'ed data in PHP? give it a value and use == or use empty() and set the value of this option to nothing, like: value="" as shown in atmon3r's answer. Commented Aug 8, 2012 at 2:46
  • May also want to look into HTML5's required attribute on the select element. Will catch some simple errors and in combination with PHP be a good experience for your user. Commented Aug 8, 2012 at 3:12

4 Answers 4

1

You can try this:

HTML

<select name="selectoption">
     <option value="">Please Select</option>
</select>

PHP

if (isset($_POST['selectoption']) && $_POST['selectoption'] == '' ) {
   echo 'Error, select an options';
   exit;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Check if $_POST is empty
Html:

<select name="category">
     <option value="">Please Select</option>
     <option value="1">Category 1</option>
     <option value="2">Category 2</option>
</select>

and php:

if (empty($_POST['category']) && isset($_POST['category'])) {
        echo 'Error, select category';
        exit;
}

Comments

0

Give it a value too, and check the value in you php with $_POST['foo']

<select name="foo">
<option value="0">Please Select</option>
...
</select>

Comments

0

For me this is better way:

if (isset($_POST['category']) && $_POST['category'] == '' ) {
       echo 'Error, select category';
       exit;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.