简体   繁体   中英

PHP Dropdown please select option

如果选择此特定值,如何使用PHP引发错误?

<option>Please Select</option>

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;
}

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;
}

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>

For me this is better way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM