如何从PHP中的DropDown列表中选择值?问题

时间:2010-04-11 10:25:42

标签: php drop-down-menu

我想知道此代码中的错误

以下代码检索数据库查询成员的名称 下拉列表

但我怎么知道你选择了谁....我只想向选择表格下拉列表的成员发送消息

    <?php
include ("connect.php");



    $name = $_POST['sector_list'];

    echo   $name  ;

?>


    <form method="POST" action="" > 
    <input type="hidden" name="sector" value="sector_list"> 
    <select name="sector_list" class="inputstandard"> 
    <option  size ="40" value="default">send to </option> 

    <?php 
        $result = mysql_query('select  * from  members  ')  
        or die (mysql_error());  

        while ($row = mysql_fetch_assoc($result)) { 

            echo '<option size ="40" value=" '. $row['MemberID'] . '" name="' . $row['MemberName']. '">' . $row['MemberName']. '</option>'; 

       } 

    ?> 

    </select> 
    </form>

我希望有人可以帮助我

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题。

<?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"> 
        <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>

要在选择某人时获得输入更改,请尝试以下操作:

<select onchange="document.getElementById('text-input').value = this.value;">
<!-- Options here -->
</select>
<input type="text" id="text-input">