Home > Article > Backend Development > How to get multiple selected values in dropdown list using php
In PHP we can use the form processing method and foreach loop to retrieve the selected values in PHP, and then output these selected values. The following article will introduce to you how to use php to obtain multiple selected values in a drop-down list. [Video tutorial recommendation: PHP tutorial]
The following is an example of how to use php to obtain multiple selected values in a drop-down list.
HTML code: First use HTML to create the selection box.
<form method = 'post' action = "demo.php"> <h4>选择课程</h4> <select name = 'subject[]' multiple size = 6> <option value = '英语'>英语</option> <option value = '数学'>数学</option> <option value = '计算机'>计算机</option> <option value = '物理学'>物理学</option> <option value = '化学'>化学</option> <option value = '经济学'>经济学</option> </select> <input type = 'submit' name = 'submit' value ='提交'> </form>
Note: If you want to select multiple values, it depends on the operating system and browser
● Window operating system: Ctrl key and mouse click
● mac operating system: command key mouse click
Rendering:
PHP code: Use PHP's form processing method and loop to retrieve in PHP Selected values, and then output these selected values.
<?php // 检查表单是否提交成功 if(isset($_POST["submit"])) { // 检查是否选中选项 if(isset($_POST["subject"])) { // 检索每个选中选项 foreach ($_POST['subject'] as $subject) print "你选择了 $subject<br/>"; } else echo "请先选中一个选项 !!"; } ?>
Rendering:
The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !
The above is the detailed content of How to get multiple selected values in dropdown list using php. For more information, please follow other related articles on the PHP Chinese website!