Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am making a drop down menu that is populated using an sql database. Using the popDropDown function below, I am taking in all the needed data and creating the needed HTML for the dynamic population.

Here's the rub... I am having a tough time returning the selected email address. I realize that the POST stuff isn't documented here that will process my request. I would like to know if there is any way to return the email address that has been selected by the user using the drop down. The end result will use the returned address + a subject and message to generate an email.

What have I forgotten to mention? Thanks is advance.

$emailAd = popDropDown($results);


function popDropDown($results){
$_email="";
while($row=$results->fetch_assoc())
{
    $email=$row['EmailAddress'];
    $id=$row['id'];
    $Name=$row['MemberName'];

    //echo $email."<br>";
    if ($_POST['emailMenu']==$id)
        {
        echo '<option value="'.$id.'"selected>'.$email."</option>";
        $_email=$email;
        }
    else
        echo '<option value="'.$id.'">'.$email."</option>";
}
return $_email;

}
share|improve this question
    
What isn't working with what you're doing now? –  jprofitt Mar 12 '12 at 14:16
    
Try echoing the relevant variables, especially $_POST['emailMenu'] and $id and seeing why they're never equal (assuming that's the problem). –  Ynhockey Mar 12 '12 at 14:17
    
@Ynhockey - I did echo the $_POST['emailMenu'] and I was returned the $id number(since $_POST['emailMenu']==$id(?)). The issue is needing to return the relevant $email address. –  bkbarton Mar 12 '12 at 14:24

1 Answer 1

up vote 0 down vote accepted

Ummm. Why do it this way? If the form being posted already contains the ID of the email record you want to use, then you do NOT need to fetch all the rows and rummage through them to find the row yo uwant, you just query directly for the ONE row you know you need:

function popDropDown() {
   $sql = "SELECT email FROM yourtable WHERE id=" . (int)$_POST['emailMenu'];
   $result = mysql_query($sql) or die(mysql_error());
   if (mysql_num_rows($result) == 0) {
       return null;
   }
   $row = mysql_fetch_assoc($result);
   return($row['email']);
}
share|improve this answer
    
Marc - I am creating a email form where the user will be able to use a drop down menu to select the recipient of an email. In your example, there isn't a creating of this drop down. Correct? –  bkbarton Mar 12 '12 at 14:29
    
Your solution did help me to find my way. Thank you. –  bkbarton Mar 22 '12 at 17:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.