tags:

views:

307

answers:

1

Hello ..

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

+1  A: 

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">
Nicklas Ansman
Thanks alotHow can I be sure that the code developed by it actually works so that I can find out who choseI did print sentence that you have choice. It did not show me any valueI want to make sure, so if you choose a particular member then this name appeare below the dropdown list . can i merge between dropdown and text box ,if chose from dropdown then the name appear in text box?
sandy
Remember to `htmlspecialchars()` the row values being added into HTML.
bobince
You'll have to do this via javascript.Example in the answer since you cannot format code here.
Nicklas Ansman