I set up a form that action="contact.php" I have this in my contact.php file <?php $to = "my@email.org"; $subject = "Registration"; $email = $_REQUEST['email'] ; $message = $_REQUEST['message'] ; $headers = "From: $email"; $sent = mail($to, $subject, $message, $headers) ; if($sent) {print "Your mail was sent successfully"; } else {print "We encountered an error sending your mail"; } ?> Code (markup): The form works fins if my only form fields are Email and Message. However, I have First Name, Last Name, country, city and a bunch of other fields people need to fill out. How do I add more lines to the php code above so it puts all the other fields in the email body? I tried this below but it didn't work?? $message = $_REQUEST['firstname'] ; $message = $_REQUEST['lastname'] ; $message = $_REQUEST['city'] ; firstname, lastname, city being the input="name" in the form.
Replace the = with .= like as follows: $message .= $_REQUEST['firstname'] ; $message .= $_REQUEST['lastname'] ; $message .= $_REQUEST['city'] ; Code (markup): If you'd like new lines for each extra bit: $message .= "\n".$_REQUEST['firstname'] ; $message .= "\n".$_REQUEST['lastname'] ; $message .= "\n".$_REQUEST['city'] ; Code (markup):
Ok, I did that and all the content showed up in the email. But in the email body, all the entries came back in 1 line of text like this 'Test 7DavidLithmanJacksonville' Instead of Test 7 David Lithman Jacksonville Anyway to fix that?
try this: $message .= "\r\n".$_REQUEST['firstname'] ; $message .= "\r\n".$_REQUEST['lastname'] ; $message .= "\r\n".$_REQUEST['city'] ; Code (markup):
Thanks frankcow! Works great. One last question though... Anyway to include the 'firstname', 'lastname', 'city' in the email body? so It would read firstname: David lastname: Lithman city: Jacksonville If not, it's ok. This should work!
Try: $message .= "\r\n".'firstname: '.$_REQUEST['firstname'] ; $message .= "\r\n".'lastname: '.$_REQUEST['lastname'] ; $message .= "\r\n".'city: '.$_REQUEST['city'] ; Code (markup):
I can do all that above and more... just with 2 lines.... even if you decided to add more options to submit in the form this will list those as well... just 2 lines! I`d replace the 3 lines with $message with this: foreach ($_REQUEST as $key => $value) $message .= "\r\n". "$key: $value"; Code (markup): Now you can post 3349329423949234 more options if you wanted to, and they`d be listed in the email. All you have to do is change the html on the form with more fields and no more touching the php. Further Note: Since this is user input sensitive, i`d probably add an array of values to check for security reasons. (as oppose to accepting all $_REQUEST values), but if you are lazy the code above works.