选择&从下拉列表中打印多个值

时间:2010-04-11 18:41:57

标签: php list drop-down-menu

在此代码中如何从下拉列表中选择多个值

<?php 


include ("connect.php");

$member_id = intval($_POST['sector_list']); 


if($member_id == 0) { 
    // Default choice was selected 
} 
else { 
    $res = mysql_query("SELECT * FROM members WHERE MemberID = $member_id LIMIT 1"); 
    if(mysql_num_rows($res) == 0) { 
        // Not a valid member 
    } 
    else { 
        // The member is in the database 
    } 
} 
?> 

<form method="post" action=""> 
    <input type="hidden" name="sector" value="sector_list">  
    <select name="sector_list" class="inputstandard" multiple="multiple">  
        <option value="0">send to</option> 
        <?php  
        $result = mysql_query('SELECT * from members') or die(mysql_error());   

        while ($row = mysql_fetch_assoc($result)) { 
            echo '<option value="' . $row['MemberID'] . '">' . $row['MemberName']. '</option>';  
        } 
        ?>  
    </select> 
</form> 

假设下拉列表包含(a,s,d,f,g,h,j,)

用户选择多个值(a,s,j)

输出= a,s,j不仅j

  
    
      

???

    
  

2 个答案:

答案 0 :(得分:3)

PHP Manual explanation

将列表名称更改为数组(最后包含[]):

<select name="sector_list[]" class="inputstandard" multiple="multiple">

$_POST['sector_list']将是所有选定选项的数组

答案 1 :(得分:1)

将选择的名称更改为:

<select name="sector_list[]" class="inputstandard" multiple="multiple">  

在你的代码中你可以得到它们:

print_r($_POST['sector_list']);