简体   繁体   中英

How do I select value from DropDown list in PHP??? Problem

I want to know the error in this code

The following code retrieves the names of the members of the database query in the dropdownlist

But how do I know who you selected.... I want to send messages only to the members that selected form dropdown list

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

I hope somebody can help me

This should do the trick.

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

To get an input to change when you select someone try this:

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

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