Jump to content

Sending an HTML form from an email address acquired from Database?


Recommended Posts

I have an HTML form that I have a "select"  drop-down menu that is selecting the name of the person you should send to. The names are being pulled from the Database, and here is what that code for the drop-down in the form looks like:

<?php  
echo '<select name= "First_Name" , "Last_Name">'; 
while( $array = mysql_fetch_assoc($result) ) { 
    $text_for_select = $array["First_Name"] . " " . $array["Last_Name"]. " " . $array["District"]; 
$value_for_select = $array["First_Name"] . " " . $array["Last_Name"] . "_" . $array["id"]; 
echo "<option></option>\n"; 
echo "<option value=\"$value_for_select\">$text_for_select</option>\n";  
} 
echo '</select>';?>

 

This works perfectly. However, what I want it to do is send the form to the email address of the person that is selected in the drop-down. The email address' are already entered in each record of the database in a field called "Email". I am using the $value_for_select variable to pull the id of the record, but I am unsure how to then tell the form to send to the email address of that record? Anybody know a way that this can be done?

 

Here is the code of the for that should be sent:

 <?php  
    if($_POST){ 
    $to = $email; 
    $subject = "WHAT SHOULD THIS BE";     
    $message  =  "Date: $date\n\r". 
                 "Dear $First_Name, $Last_Name,\n\r". 
                 "Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah..\n\r". 
                 
                "Sincerely,\n". 
                "$name \n". 
                "$street \n". 
                "$city, $zip \n". 
                "$email \\n". 
    $headers = "From: $email"; 
    mail($to, $subject, $message, $headers); 
    // SUCCESS! 
    echo '<p class="notice">'. 
        'Thank you for your submission.  '. 
          '</p>'; 
    // clear out the variables for good-housekeeping 
    unset($date,$legislator,$bill,$name,$street,$city,$zip,$email); 
    $_POST = array(); 
} 
?>

 

Please help!

 

 

Can you use :

 

$value_for_select = $array["Email"] ?.

 

 

 

How would that work? Isn't that the same thing I am doing with the id? (but your is a more direct approach?) And how can I tell the for to send to that address?

$from_address=noreply@yourdomain.com //this is going to be same for all recipient ? or else you can pass the from_address as hidden form field.

<input type="hidden" value="noreply@youdomain.com" name="from_address" />

-

My approach will allow you get the email address directly and you don't have to query the DB again.

 

 

 

 

I'm sorry. I screwed up when naming this thread. I am trying to send it TO the selected person email address, not From. So I am not overly concerned on the "From" field, but want to populate the the $to field with the mail address of the person that is selected in the drop-down. ANy ideas on how to so that?

 

from your other thread:

 

just one thing: this will create an invalid select:

 

echo '<select name= "First_Name" , "Last_Name">';

 

The name value for a form element should be a single, quoted string immediately following the equal sign. so something like this would work:

 

echo '<select name="First_and_Last_Name">';

 

If you want to send an email to the selected person, you should user their record id as the value for each <option>, then when the form is submitted you can get the posted id and look up the email address using that id.

modify

echo '<select name= "displayname">';

$value_for_select = $array["Email"]

echo "<option value=\"$value_for_select\">$text_for_select</option>\n"; 

 

in other part

<?php 

    if($_POST){

    $to = $_POST['displayname']; //this will give you the to email address

    $subject = "WHAT SHOULD THIS BE";   

    $message  =  "Date: $date\n\r".

                "Dear $First_Name, $Last_Name,\n\r".

                "Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah..\n\r".

               

                "Sincerely,\n".

                "$name \n".

                "$street \n".

                "$city, $zip \n".

                "$email \\n".

    $headers = "From: $email";

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

    // SUCCESS!

    echo '<p class="notice">'.

        'Thank you for your submission.  '.

          '</p>';

    // clear out the variables for good-housekeeping

    unset($date,$legislator,$bill,$name,$street,$city,$zip,$email);

    $_POST = array();

}

?>

 

 

to understand more what your $_POST contains try print_r($_POST)

modify

echo '<select name= "displayname">';

$value_for_select = $array["Email"]

echo "<option value=\"$value_for_select\">$text_for_select</option>\n"; 

 

in other part

<?php 

    if($_POST){

    $to = $_POST['displayname']; //this will give you the to email address

    $subject = "WHAT SHOULD THIS BE";   

    $message  =  "Date: $date\n\r".

                "Dear $First_Name, $Last_Name,\n\r".

                "Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah..\n\r".

               

                "Sincerely,\n".

                "$name \n".

                "$street \n".

                "$city, $zip \n".

                "$email \\n".

    $headers = "From: $email";

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

    // SUCCESS!

    echo '<p class="notice">'.

        'Thank you for your submission.  '.

          '</p>';

    // clear out the variables for good-housekeeping

    unset($date,$legislator,$bill,$name,$street,$city,$zip,$email);

    $_POST = array();

}

?>

 

 

to understand more what your $_POST contains try print_r($_POST)

 

That is fantastic. now I have a much better idea of what needs to happen. I have modified the code as per your instructions, however it is still not passing the email. Here is what I changed it to be. What did I do wrong?

<?php 
echo '<select name="First_and_Last_Name">';
while( $array = mysql_fetch_assoc($result) ) {
$text_for_select = $array["First_Name"] . " " . $array["Last_Name"]. " " . $array["District"];
$value_for_select = $array["id"];
echo "<option></option>\n";
echo "<option value=\"$value_for_select\">$text_for_select</option>\n"; 
}
echo '</select>';?>

I see

 

$value_for_select = $array["id"];

 

is id is email address??

 

in php code modify $to = $_POST['displayname']; to $to = $_POST['First_and_Last_Name'];

 

 

Thank you!!! That seemed to work. Now I have the form working.  Here is the final code!

 


<?php 
echo '<select name="First_and_Last_Name">';
while( $array = mysql_fetch_assoc($result) ) {
$text_for_select = $array["First_Name"] . " " . $array["Last_Name"]. " " . $array["District"];
$value_for_select = $array["Email"];
echo "<option></option>\n";
echo "<option value=\"$value_for_select\">$text_for_select</option>\n"; 
}
echo '</select>';?>

 

And the $to field is formatted as:

 

 

$to = $_POST['First_and_Last_Name'];

 

 

Thank you sooooooo much!

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.