简体   繁体   中英

Multiple select option into a PHP array

I have a selectbox list. Is it possible to select multiple options?

<select name="access_list[ ]" size="7" multiple="multiple">
    <?php $res=mysql_query("select * from list" ,$conn);
    while($row=mysql_fetch_assoc($res))
    echo"<option value=".$row['id'].">".$row['name']."</option>";?>
</select>

How are the values ​that will be selected (select multiple values ​together) stored in the array?

Use name as name="access_list[]" without space.

And you can get selected options with $_POST['access_list']

$_POST['access_list'] is array that contains selected options

Replace your select tag with this:

<select name="access_list[]" size="7" multiple="multiple">

If you want to get the array, you can do it like this:

$data = $_POST['access_list'];
print_r($data);

store as array then in your php is like this.

<?php

    $access_list = $_POST['access_list'];

    foreach($access_list as $value)
    {
        //Do your code Here
    }


?>

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