PHP HTML Dropdown List
From w3cyberlearnings
Contents |
PHP works with HTML Select element
HTML Select element uses to generate a drop down list. A drop down list provides users many options to select from.
Syntax Select
<select name="NAME"> <option value="v1">V1</option> <option value="v2">V2</option> <option value="v3">V3</option> </select>
Related Information
HTML tutorial for select drop down list and group related option for drop down list.
Example 1
<?php if ($_POST) { echo '<pre>'; print_r($_POST); echo '</pre>'; } ?> <html> <head> <title>Select Drop Down List</title> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> My Major:<br/> <select name="major"> <option value="computer science">Computer Science</option> <option value="English">English</option> <option value="Business">Business</option> <option value="Sport">Sport</option> <option value="Maths">Maths</option> </select><br/> Courses(Multiple Select):<br/> <select name="courses[]" multiple="multiple"> <option value="eng101">English 101</option> <option value="math201">Math 201</option> <option value="cs201">C++ Foundation</option> <option value="bs201">Business 201</option> </select><br/> <input type="submit"/> </form> </body> </html>
Output
Example 2
<?php $msg = ""; if (!empty($_POST['submit'])) { if (!empty($_POST['age'])) { foreach ($_POST['age'] as $g) { $msg .='<li>' . $g . '--> age group </li>'; } } if (!empty($_POST['season'])) { foreach ($_POST['season'] as $s) { $msg .='<li>' . $s . '--> season </li>'; } } if (!empty($_POST['fruit'])) { $msg .='<li>' . $_POST['fruit'] . '--> fruit </li>'; } } ?> <html> <head> <title>Multiple Value</title> </head> <body> <?php echo '<ul>' . $msg . '</ul>'; ?> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <i>Age Options(Multiple Selection):</i><br/> <select name="age[]" multiple size="4"> <option value="age_group1">Age group 1</option> <option value="age_group2">Age group 2</option> <option value="age_group3">Age group 3</option> <option value="age_group4">Age group 4</option> <option value="age_group5">Age group 5</option> <option value="age_group6">Age group 6</option> </select> <br/> <i>Seasons:</i><br/> <input type="checkbox" name="season[]" value="Spring"/>Spring<br/> <input type="checkbox" name="season[]" value="Summer"/>Summer<br/> <input type="checkbox" name="season[]" value="Autumn"/>Autumn<br/> <input type="checkbox" name="season[]" value="Winter"/>Winter <br/> <i>Fruits:</i><br/> <input type="radio" name="fruit" value="Apple"/>Apple<br/> <input type="radio" name="fruit" value="Banna"/>Banana<br/> <input type="radio" name="fruit" value="Orange"/>Orange<br/> <input type="radio" name="fruit" value="Graph"/>Graph<br/> <input type="submit" name="submit" value="Multiple Value"/> </form> </body> </html>
Output
Related Links
- Drop down list
- Pass value uses URL
- Pass value uses URL 2.
- Multiple Submit Buttons