TWyPGn Posted September 23, 2009 Report Share Posted September 23, 2009 Hello, I am making a contact form that takes input from the contact form and sends it to you in an email message. The HTMl is: Email: Message: The PHP of "contactform" is: <?php $email = $_REQUEST['email']; $message = $_REQUEST['message'] ; mail("email@yahoo.com", "Message from the Website", $message, "From: $email" ); header( "Location: http://www.website.com/thanks.html\" ); ?> I need to add more fields to the form such as Name, Address and Telephone but being this my first experience with PHP I am unable to do that. Upon adding additional fields the form seems to work fine as the form moves to thanks.html page but there is no email sent. Please help me add multiple fields to the form above that actually sends the data in the email. Thanks! Quote Link to comment Share on other sites More sharing options...
falkencreative Posted September 23, 2009 Report Share Posted September 23, 2009 Here's a quick example with one additional form element added: ></pre> <form method="post" action="contactform.php"> Email: Name: Message: </ PHP: $email = $_REQUEST['email']; $message = "Name: " . $_REQUEST['name'] . "/n" . "Message: " . $_REQUEST['message']; mail("email@yahoo.com", "Message from the Website", $message, "From: $email" ); header( "Location: http://www.website.com/thanks.html" ); ?> The "name" element of the in the form needs to match the $_REQUEST[] in the PHP. So, since I added name="name" to the input, I have to use $_REQUEST['name']. This line: $message = "Name: " . $_REQUEST['name'] . "/n" . "Message: " . $_REQUEST['message']; creates a string that should look like this: Name: [name] Message: [message] The "\n" portion creates a new line. Quote Link to comment Share on other sites More sharing options...
dms Posted September 24, 2009 Report Share Posted September 24, 2009 Since you are using $_POST in your form, you might be better served to use it in your PHP script to capture the user input instead of $_REQUEST. Ben example looks good to me, but I'm curious about one item in the mail(). [mail("email@yahoo.com", "Message from the Website", $message, "From: $email" );] Does ["From: $email"] need to be ["From:" . $email]? Quote Link to comment Share on other sites More sharing options...
falkencreative Posted September 24, 2009 Report Share Posted September 24, 2009 @dms Since you are using $_POST in your form, you might be better served to use it in your PHP script to capture the user input instead of $_REQUEST. Probably true. Using $_POST is a bit more secure than $_REQUEST, though both will work. "Note: The variables in $_REQUEST are provided to the script via the GET, POST, and COOKIE input mechanisms and therefore could be modified by the remote user and cannot be trusted. The presence and order of variables listed in this array is defined according to the PHP variables_order configuration directive. " http://ca.php.net/manual/en/reserved.variables.request.php [mail("email@yahoo.com", "Message from the Website", $message, "From: $email" );] Does ["From: $email"] need to be["From:" . $email]? Either will work. The first example will work as long as double quotes are used. This, however, won't work (using single quotes): mail("email@yahoo.com", "Message from the Website", $message, 'From: $email'); If double quotes are used, PHP understands to check inside the string for variables. If single quotes, it assumes that it is a simple string that includes no variables. Quote Link to comment Share on other sites More sharing options...
jlhaslip Posted September 24, 2009 Report Share Posted September 24, 2009 Just wanted to add that using the single quote method actually parses the form "somewhat" quicker. Not a big deal for a small script like this, but it makes a difference an a larger script. Might as well develop some good programming skills now as a matter of habit. Then you be a better scripter in the long run. Quote Link to comment Share on other sites More sharing options...
falkencreative Posted September 24, 2009 Report Share Posted September 24, 2009 Just wanted to add that using the single quote method actually parses the form "somewhat" quicker.Not a big deal for a small script like this, but it makes a difference an a larger script. Might as well develop some good programming skills now as a matter of habit. Then you be a better scripter in the long run. Agreed. I just didn't want to throw too much at a beginner all at once. :/ Quote Link to comment Share on other sites More sharing options...
TWyPGn Posted September 24, 2009 Author Report Share Posted September 24, 2009 Hello & Thanks to falkencreative, dms, jlhaslip for relpying to my query. I did try this code: $message = "Name: " . $_REQUEST['name'] . "/n" . "Message: " . $_REQUEST['message']; The email is sent with the info but there is one problem. Instead of another line, the message is displayed in the same line as the name is with \n. Something like this: Name: TWyPGn/nMessage: hello Would you please help me solve this problem? Thanks! Quote Link to comment Share on other sites More sharing options...
falkencreative Posted September 24, 2009 Report Share Posted September 24, 2009 Oops. I noticed this issue earlier, but I thought I edited my post... Instead of "/n" try "\n". That should fix it. Quote Link to comment Share on other sites More sharing options...
TWyPGn Posted September 24, 2009 Author Report Share Posted September 24, 2009 Thanks falkencreative! By changing "/n" to "\n" has created another problem: The email is not sent at all I don't know why because I just changed this, nothing else. I am so sorry to bother you with such little problems but being a novice in PHP I am unable to fix such problems. I'll appreciate if you could please help me resolve the issue. Thanks! Quote Link to comment Share on other sites More sharing options...
falkencreative Posted September 24, 2009 Report Share Posted September 24, 2009 I'd suggest waiting a little longer... it may just be that the server is just taking a while to send it. I'm using "\n" in a quick test script, and it works fine. If you are still having issues, and you've waited about half an hour, post the PHP code you are using -- perhaps there is something else up with it. Quote Link to comment Share on other sites More sharing options...
TWyPGn Posted September 24, 2009 Author Report Share Posted September 24, 2009 Hi, thanks for your help so far! The code I am using is as follows: <?php $email = $_REQUEST['email'] ; $to = "email@yahoo.com"; $subject = "Message from website"; $message = "Name: " . $_REQUEST['name'] . "\n" . "Address: " . $_REQUEST['address'] . "\n" . "Telephone: " . $_REQUEST['telephone'] . "\n" . "Message: " . $_REQUEST['message']; mail( $to, $subject, $message,"From: $email" ); header( "Location: http://www.website.com/thanks.html\" ); ?> I just need to have Name, Address, Telephone and Message displayed in new lines in the body of the email message. I don't know if there is anything else that can take care of that. I would appreciate if you could please help me in making this form/code work fine. Thanks! Quote Link to comment Share on other sites More sharing options...
falkencreative Posted September 24, 2009 Report Share Posted September 24, 2009 That code works fine for me... I receive the email about a minute after completing the form, and it has the correct new lines. Perhaps a silly question, but you've changed the $email value from "email@yahoo.com" to your own email address, correct? Quote Link to comment Share on other sites More sharing options...
TWyPGn Posted September 24, 2009 Author Report Share Posted September 24, 2009 Yes, I have my own email address in the $to field:). I receive the email when I use "/n", of course it displays all entries in one line with /n between them. When I change "/n" to "\n" I do not get any email. Don't know what's the problem. May be the code doesn't want to work today:/ Quote Link to comment Share on other sites More sharing options...
falkencreative Posted September 24, 2009 Report Share Posted September 24, 2009 Ummm... a bit of a guess on my part... you could try using "\r\n" instead, in case the server is having issues with the new line character. Otherwise, you could use create an HTML mail (rather than plain text, as you are doing now) by looking at the guide here: http://www.w3schools.com/PHP/func_mail_mail.asp (example 3) That would allow you to use a line break () rather than a new line character. Quote Link to comment Share on other sites More sharing options...
TWyPGn Posted September 24, 2009 Author Report Share Posted September 24, 2009 Ahhhh.... It finally works fine! Thank you so much falkencreative for your help and time. I really appreciate it. Thanks! Quote Link to comment Share on other sites More sharing options...
falkencreative Posted September 24, 2009 Report Share Posted September 24, 2009 No problem... glad you got it working. Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.