lazerbrains Posted December 21, 2010 Share Posted December 21, 2010 I have an email form that is working wonderfully. I have a select box that has a list of names of people that you can email. That list is being pulled from a database. It is currently sending to the email related to the name selected in the dropdown menu. However, in the email that sends, I would like the name of the recipient to show up in the greeting. I.E. Dear "name from dropdown". How do I go about doing this. My select box that holds the query for the name is as folows: <?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>';?> No, this is querying the First and Last Name and another variable called district in to a new variable called "text_for_select". I just want to either print the "text_for_select" after the "Dear", or better yet, just the "First_Name" and "Last_Name". How would I get this to print there? Quote Link to comment https://forums.phpfreaks.com/topic/222269-posting-name-from-database-into-the-greeting-of-email-letter/ Share on other sites More sharing options...
denno020 Posted December 21, 2010 Share Posted December 21, 2010 echo "Dear" . $array["First Name"] . " " . $array["Last Name"] . ","; tried that? (quotes could be wrong, but that's the general form I think should work..) Denno Quote Link to comment https://forums.phpfreaks.com/topic/222269-posting-name-from-database-into-the-greeting-of-email-letter/#findComment-1149787 Share on other sites More sharing options...
lazerbrains Posted December 22, 2010 Author Share Posted December 22, 2010 OK. I did what you said and this is the code. It still sends the email fine, but I get blank space after the "Dear". Here's the code: "Dear " . $array["First_Name"] . " " . $array["Last_Name"]. ",\n\r". Is something wrong with my syntax. Does anyone have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/222269-posting-name-from-database-into-the-greeting-of-email-letter/#findComment-1150124 Share on other sites More sharing options...
denno020 Posted December 22, 2010 Share Posted December 22, 2010 Ok so I had a bit of a play around, and my next question would be, are you sure you've put the values into the array correctly? I checked online, and found out to input the values, and then I simply echoed out one value at a time, and it worked fine, which means it will print into your email fine.. For testing, I would try echoing just First Name, and then just Last Name, separately, and see if it prints anything. This way you can see if the data is going into the array fine. If the values print as expected, then the string you'r building for the email is being built incorrectly. Here is the code I made, which worked perfectly fine. $arr = array("FirstName" => "John", "LastName" => "Doe", "Age" => "23"); echo $arr["FirstName"]; echo "\n"; echo $arr["LastName"]; echo "\n"; echo $arr["Age"]; Quote Link to comment https://forums.phpfreaks.com/topic/222269-posting-name-from-database-into-the-greeting-of-email-letter/#findComment-1150140 Share on other sites More sharing options...
lazerbrains Posted December 22, 2010 Author Share Posted December 22, 2010 Well the array is already pulling the correct values from the database. I is pulling them in the Form: <form method="post" action="" id="letter"> <p><label for="date">Date</label> <input type="text" name="date" id="date" value="<?php echo $_POST['date'];?>"/></p> <p>Dear Person, <?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>';?> </p> That is currently pulling the First Name and Last name into a drop down menu. It is working fine. I just want to pull the name of the person selected into the Greeting of the email. SO that it says "Dear 'Name from $text_for_select'. Isn't there a better way to do that? Quote Link to comment https://forums.phpfreaks.com/topic/222269-posting-name-from-database-into-the-greeting-of-email-letter/#findComment-1150145 Share on other sites More sharing options...
denno020 Posted December 22, 2010 Share Posted December 22, 2010 Did you try testing by just echoing one part of the name? To build ur Dear part do this: echo "Dear "; echo "$array["First_Name"]; echo " "; //space echo "$array["Last_Name"]; that should work... Try it and let me know. Denno Quote Link to comment https://forums.phpfreaks.com/topic/222269-posting-name-from-database-into-the-greeting-of-email-letter/#findComment-1150152 Share on other sites More sharing options...
lazerbrains Posted December 22, 2010 Author Share Posted December 22, 2010 I did that. Added into this: <?php if($_POST){ $to = $_POST['First_and_Last_Name']; $subject = "WHAT SHOULD THIS BE"; $message = "Date: $date\n\r". echo "Dear "; echo "$array["First_Name"]; echo " "; //space echo "$array["Last_Name"]; "" .$_POST['comment']. "\n\r". "Sincerely,\n". "From: " .$_POST['from']. "\n". "Street: " .$_POST['street']. "\n". "City: " .$_POST['city']. "\n". "Zip: " .$_POST['zip']. "\n". "Email: " .$_POST['email']. "\n". $headers = "From: " .$_POST['from']. "<".$_POST['email'].">"."\n"; 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,$bill,$name,$street,$city,$zip,$email); $_POST = array(); } ?> and got this: Parse error: syntax error, unexpected T_ECHO in /home5/windfal2/public_html/ledge/wp-content/themes/arras/letter.php on line 31 Still sent when the code was this: <?php if($_POST){ $to = $_POST['First_and_Last_Name']; $subject = "WHAT SHOULD THIS BE"; $message = "Date: $date\n\r". "Dear " . $array["First_Name"] . " " . $array["Last_Name"]. ",\n\r". "" .$_POST['comment']. "\n\r". "Sincerely,\n". "From: " .$_POST['from']. "\n". "Street: " .$_POST['street']. "\n". "City: " .$_POST['city']. "\n". "Zip: " .$_POST['zip']. "\n". "Email: " .$_POST['email']. "\n". $headers = "From: " .$_POST['from']. "<".$_POST['email'].">"."\n"; 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,$bill,$name,$street,$city,$zip,$email); $_POST = array(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/222269-posting-name-from-database-into-the-greeting-of-email-letter/#findComment-1150156 Share on other sites More sharing options...
denno020 Posted December 22, 2010 Share Posted December 22, 2010 Can you post that code again with the number 31 written at the start of line 31 please. Denno Quote Link to comment https://forums.phpfreaks.com/topic/222269-posting-name-from-database-into-the-greeting-of-email-letter/#findComment-1150157 Share on other sites More sharing options...
lazerbrains Posted December 22, 2010 Author Share Posted December 22, 2010 <?php if($_POST){ $to = $_POST['First_and_Last_Name']; $subject = "WHAT SHOULD THIS BE"; $message = "Date: $date\n\r". LINE 31 echo "Dear "; echo "$array["First_Name"]; echo " "; //space echo "$array["Last_Name"]; "" .$_POST['comment']. "\n\r". "Sincerely,\n". "From: " .$_POST['from']. "\n". "Street: " .$_POST['street']. "\n". "City: " .$_POST['city']. "\n". "Zip: " .$_POST['zip']. "\n". "Email: " .$_POST['email']. "\n". $headers = "From: " .$_POST['from']. "<".$_POST['email'].">"."\n"; 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,$bill,$name,$street,$city,$zip,$email); $_POST = array(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/222269-posting-name-from-database-into-the-greeting-of-email-letter/#findComment-1150160 Share on other sites More sharing options...
denno020 Posted December 22, 2010 Share Posted December 22, 2010 Nevermind, I copied it into Dreamweaver and fixed the problem: if($_POST){ $to = $_POST['First_and_Last_Name']; $subject = "WHAT SHOULD THIS BE"; $message = "Date: $date\n\r"; echo "Dear "; echo $array["First_Name"]; echo " "; echo $array["Last_Name"]; "" .$_POST['comment']. "\n\r". "Sincerely,\n". "From: " .$_POST['from']. "\n". "Street: " .$_POST['street']. "\n". "City: " .$_POST['city']. "\n". "Zip: " .$_POST['zip']. "\n". "Email: " .$_POST['email']. "\n". $headers = "From: " .$_POST['from']. "<".$_POST['email'].">"."\n"; 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,$bill,$name,$street,$city,$zip,$email); $_POST = array(); } I got my quotes wrong , sorry lol. Denno Quote Link to comment https://forums.phpfreaks.com/topic/222269-posting-name-from-database-into-the-greeting-of-email-letter/#findComment-1150161 Share on other sites More sharing options...
lazerbrains Posted December 22, 2010 Author Share Posted December 22, 2010 I tried that, but with that code, the email sent has only this content: Date: There is no error message, but I get less transmitted than the original. Weird. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/222269-posting-name-from-database-into-the-greeting-of-email-letter/#findComment-1150163 Share on other sites More sharing options...
denno020 Posted December 22, 2010 Share Posted December 22, 2010 Remove the semi-colon's after each of my lines too. They're terminating the assignment of $message too early. Replace with the full stop or whatever the correct syntax is. Denno Quote Link to comment https://forums.phpfreaks.com/topic/222269-posting-name-from-database-into-the-greeting-of-email-letter/#findComment-1150164 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.