How to get multiple selected values ​​in dropdown list using php

青灯夜游
Release: 2023-04-04 22:24:01
Original
4743 people have browsed it

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]

How to get multiple selected values ​​in dropdown list using php

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 = &#39;post&#39; action = "demo.php">  
        <h4>选择课程</h4> 
          
        <select name = &#39;subject[]&#39; multiple size = 6>   
            <option value = &#39;英语&#39;>英语</option> 
            <option value = &#39;数学&#39;>数学</option> 
            <option value = &#39;计算机&#39;>计算机</option> 
            <option value = &#39;物理学&#39;>物理学</option> 
            <option value = &#39;化学&#39;>化学</option> 
            <option value = &#39;经济学&#39;>经济学</option> 
        </select> 
        <input type = &#39;submit&#39; name = &#39;submit&#39; value =&#39;提交&#39;> 
</form>
Copy after login

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:

How to get multiple selected values ​​in dropdown list using php

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[&#39;subject&#39;] as $subject)  
                print "你选择了 $subject<br/>"; 
        } 
    else
        echo "请先选中一个选项 !!"; 
    } 
?>
Copy after login

Rendering:

How to get multiple selected values ​​in dropdown list using php

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template