lazerbrains Posted December 13, 2010 Share Posted December 13, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/221558-sending-an-html-form-from-an-email-address-acquired-from-database/ Share on other sites More sharing options...
priti Posted December 13, 2010 Share Posted December 13, 2010 Can you use : $value_for_select = $array["Email"] ?. Quote Link to comment https://forums.phpfreaks.com/topic/221558-sending-an-html-form-from-an-email-address-acquired-from-database/#findComment-1146901 Share on other sites More sharing options...
lazerbrains Posted December 13, 2010 Author Share Posted December 13, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/221558-sending-an-html-form-from-an-email-address-acquired-from-database/#findComment-1146905 Share on other sites More sharing options...
priti Posted December 13, 2010 Share Posted December 13, 2010 $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. Quote Link to comment https://forums.phpfreaks.com/topic/221558-sending-an-html-form-from-an-email-address-acquired-from-database/#findComment-1146914 Share on other sites More sharing options...
lazerbrains Posted December 13, 2010 Author Share Posted December 13, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/221558-sending-an-html-form-from-an-email-address-acquired-from-database/#findComment-1146926 Share on other sites More sharing options...
BlueSkyIS Posted December 13, 2010 Share Posted December 13, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/221558-sending-an-html-form-from-an-email-address-acquired-from-database/#findComment-1146930 Share on other sites More sharing options...
priti Posted December 13, 2010 Share Posted December 13, 2010 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) Quote Link to comment https://forums.phpfreaks.com/topic/221558-sending-an-html-form-from-an-email-address-acquired-from-database/#findComment-1146934 Share on other sites More sharing options...
lazerbrains Posted December 13, 2010 Author Share Posted December 13, 2010 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>';?> Quote Link to comment https://forums.phpfreaks.com/topic/221558-sending-an-html-form-from-an-email-address-acquired-from-database/#findComment-1146939 Share on other sites More sharing options...
priti Posted December 13, 2010 Share Posted December 13, 2010 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']; Quote Link to comment https://forums.phpfreaks.com/topic/221558-sending-an-html-form-from-an-email-address-acquired-from-database/#findComment-1146942 Share on other sites More sharing options...
lazerbrains Posted December 14, 2010 Author Share Posted December 14, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/221558-sending-an-html-form-from-an-email-address-acquired-from-database/#findComment-1146952 Share on other sites More sharing options...
priti Posted December 14, 2010 Share Posted December 14, 2010 You most welcome buddy Quote Link to comment https://forums.phpfreaks.com/topic/221558-sending-an-html-form-from-an-email-address-acquired-from-database/#findComment-1146960 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.