Jump to content

Archived

This topic is now archived and is closed to further replies.

kenwvs

Sending an email using a dropdown box

Recommended Posts

I have a form that gets completed and then I want to email the results of the form to different people.  The person completing the form will choose who they want to send the results to.  I have a dropdown box, where the list of names used is obtained from the database.

Is there a way, that when a person hilites multiple names that the email can be sent to these people.

Here is what I have for the dropdown box.
[CODE]<div>
                                        <center><B>Send this form to:</B><BR>
                                        <select name='tech' multiple>

                                        <?php

    $result = mysql_query("SELECT * FROM employees") or die(mysql_error());
    while ($row = mysql_fetch_assoc($result)) {
        echo "<option>$row[name]</option>";
    }[/CODE]

Once you have selected who the email goes to, and completed the form, I want to create a query that will send the email to these people when you press the submit button for the rest of the form.  The data is being saved in a database, but I don't have it set up to save the names of the people receiving an email (although I could if necessary.)

Thanks for any help as this has me totally stumped.

Ken

Share this post


Link to post
Share on other sites
The name of the select needs to be an array ... fully explained with example code at http://onlinetools.org/tricks/using_multiple_select.php

Share this post


Link to post
Share on other sites
If I use an array and do this, I will end up with something like this..........
[code]<div>
                                        <center><B>Send this form to:</B><BR>
                                        <select name='emailname[]' multiple="multiple">

                                        <?php

    $result = mysql_query("SELECT * FROM employees") or die(mysql_error());
    while ($row = mysql_fetch_assoc($result)) {
        echo "<option>$row[name]</option>";
    }
$emailname=$_POST['emailname'];
if ($emailname){
foreach ($emailname as $ename); }

    ?></select></center>[/code]

Would I then be able to do something like this to get the email addresses into the TO field of the email script:

SELECT email from employees where $ename=name;

email is the column name in the DB table and name is the column title where the name is found.

Thanks,

Ken

Share this post


Link to post
Share on other sites
i dont get it do you mean this select the members emails withn a pualldown list then be able to use the email of the user?

example
[code]
<div>
                                        <center><B>Send this form to:</B><BR>
                                        <select name='emailname[]' multiple="multiple">

                                        <?php

    $result = mysql_query("SELECT email FROM employees") or die(mysql_error());
    while ($row = mysql_fetch_assoc($result)) {
        echo "<option>$row[email]</option>";
    }

?></select></center>
[/code]

might be wrong here only guessing.

Share this post


Link to post
Share on other sites
The dropdown box has a list of names (not email addresses) that it has gotten from the employee table of the database.  Once you select which names you want to send an email to, I need to get the email addresses from the employee table, to actually send the email.

Share this post


Link to post
Share on other sites
so dont you just say

$row['name']=$row['email'];

then the email code

[code]
<div>
                                        <center><B>Send this form to:</B><BR>
                                        <select name='emailname[]' multiple="multiple">

                                        <?php

    $result = mysql_query("SELECT * FROM employees") or die(mysql_error());
    while ($row = mysql_fetch_assoc($result)) {
        echo "<option>$row[name]</option>";
        $row['name']=$row['email'];
}
?></select></center>
[/code]

Share this post


Link to post
Share on other sites
I will try that.  I have no idea how to do this, as I am new at all of this.  I have never used the mail function, and this is the first DB project I have ever worked on.

Share this post


Link to post
Share on other sites
i thort that you select the users name then you do another selct email from users were username is ='$name'

then the email code sure of it.

Share this post


Link to post
Share on other sites
some think like this i am sure that when you select a name you then convert the name intot the users email address then send the email via the user name as it is there email address.
example only
[code]
<div>
<center><B>Send this form to:</B><BR>
<select name='emailname[]' multiple="multiple">

<?php

$result = mysql_query("SELECT * FROM employees") or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo "<option>$row[name]</option>";
    }
foreach ($emailname as $ename);

$ename=$row['email'];

</select></center>

$to      = $ename;

$subject = 'the subject';

$message = 'hello';

$headers = 'From: webmaster@example.com' . "\r\n" .
 
'Reply-To: webmaster@example.com' . "\r\n" .
 
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

?>
[/code]

Share this post


Link to post
Share on other sites
Ok, I see what you are saying.  Thanks a bunch for the help.  It is starting to make sense now.  I appreciate your help.

Ken

Share this post


Link to post
Share on other sites
this should defently work
[code]
<div>
<center><B>Send this form to:</B><BR>
<select name='emailname[]' multiple="multiple">

<?php

$result = mysql_query("SELECT * FROM employees") or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo "<option>$row[name]</option>";
   }
foreach($emailname as $ename);


$result2 = mysql_query("SELECT email FROM employees where ename='$ename'") or die(mysql_error());

while ($a = mysql_fetch_assoc($result2)) {

$email=$a['email'];

</select></center>

$to      = $email;

$subject = 'the subject';

$message = 'hello';

$headers = 'From: webmaster@example.com' . "\r\n" .
 
'Reply-To: webmaster@example.com' . "\r\n" .
 
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

}
?>
[/code]

Share this post


Link to post
Share on other sites
I have tried using the last script supplied, but for some reason it isn't sending an email.  I have also added the line [code=php:0]echo 'Email has been sent to'.$_POST[$ename];[/code] to see what it shows and it prints out the Email has been sent to, but there is no names in there.

Am I missing a step as far as setting the email address of something for it to use it?  I am not really sure what I would need to change to make this actually send the email.  the rest of the form is being saved to the Db, so it is making a connection and not generating any errors.

Any ideas or suggestions would be appreciated.  Would I need to save the names to a db table before it could send the email?

Thanks,

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.