Jump to content
Killersites Forums

Need help in "Contact Form" code


TWyPGn
 Share

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]?

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. :/

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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:/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
 Share

×
×
  • Create New...