View Full Version : Sending form data to email with PHP--having trouble!!!!

03-17-2010, 09:34 PM
I am trying to make a simple form for people to fill out, and I want the data entered in the form to be sent to my email address when users click the submit button. I have gotten to where I get an email, but the email is blank!! I have been working with this for about two weeks, trying to get it to work, and I am getting really frustrated.

Here is the HTML code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Transitional//EN" "(show_form.html)">
<title>E-Mail Form</title>

<form method="post" action="do_sendform.php3">

<p>Your Name:
<br />
<input type="text" name="sender_name" size=30>

<p>Your E-Mail Address:
<br />
<input type="text" name="sender_email" size=30>

<br />
<textarea name="message" cols=30 rows=5></textarea>

<input type="submit" value="Send This Form">



and here is the PHP code:


$msg = "Sender Name:\t$sender_name\n";
$msg .= "Sender E-Mail:\t$sender_email\n";
$msg .= "Message:\t$message\n\n";

$recipient = "myemailaddress@mysite.net";
$subject = "Web Site Feedback";

$mailheaders = "From: My Web Site <> \n";
$mailheaders .= "Reply-To: $sender_email\n\n";

mail($recipient, $subject, $msg, $mailheaders);

echo "<html><head><title>Form Sent!</title></head></body>";
echo "<h1 align=center>Thank You, $sender_name</h1>";
echo "<p align=center>Your feedback has been sent.</p>";
echo "</body></html>";


03-17-2010, 09:39 PM
You need to read your form variables ... using $_POST as specified in your <form> ...

$msg = "Sender Name:\t $_POST['sender_name'] \n";
$msg .= "Sender E-Mail:\t $_POST['sender_email'] \n";
$msg .= "Message:\t $_POST['message'] \n\n";

I won't even go into captcha, validation, or other security issues.
You're doing just the basic email "as-is".


03-17-2010, 09:42 PM
PHP3 regiser_global reliance. At least this will be gone soon :D
There is no such thing as $sender_name, $sender_email, or $message. Extract those out of the $_POST superglobal array: $_POST['sender_name'], $_POST['sender_email'], $_POST['message'].

Not sure what you're doing with the track_vars there, but best I know that directive hasn't existed since like 4.1. That was used to push information into the _* superglobals from the HTTP_*_VARS, but hasn't been necessary since as of 4.1 the track_vars was enabled by default (and best I know, cannot be disabled). Its been soooooooo long since I've used that though, so I can't be 100% certain but I believe that it was an INI_PER_DIR directive, not an INI_ALL directive. So that wouldn't have even worked at the time embedded in a script :$

Just checked the API on that one. Track_vars was removed in 4.0.3, and has an unknown PHP_INI_ALL directive. So it would work if properly used at the time embedded directly into a script.

03-17-2010, 10:25 PM
I know very little about PHP, I got this code of some coding website......didn't know the "php_vars" tag was outdated! :o
I tried the code the first poster mentioned (name slips my mind) and it came up with this error message:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/a3864192/public_html/do_sendform.php3 on line 3

Like I said, I know very little about PHP, so I don't understand this.

03-17-2010, 10:40 PM
Complexity issue on the use of arrays. A few approaches:

$msg = "Sender Name:\t {$_POST['sender_name']} \n";
$msg .= "Sender E-Mail:\t {$_POST['sender_email']} \n";
$msg .= "Message:\t {$_POST['message']} \n\n";


$msg = "Sender Name:\t " . $_POST['sender_name'] . "\n";
$msg .= "Sender E-Mail:\t " . $_POST['sender_email'] . "\n";
$msg .= "Message:\t " . $_POST['message'] . "\n\n";

or (probably my favorite, I like using format options):

$msg = sprintf("Sender Name:\t%s\n", $_POST['sender_name']);
$msg .= sprintf("Sender E-Mail:\t%s\n", $_POST['sender_email']);
$msg .= sprintf("Message:\t%s\n\n", $_POST['message']);

Next time you search for any scripts in php, I'd recommend searching for "PHP5 scripttype". That should reduce the number of PHP3 popups you get, which is long long outdated (PHP4 is getting very close).

03-17-2010, 10:59 PM
THANK YOU SO MUCH Fou-lu!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! It worked!!!

03-18-2010, 03:18 AM
oops ... I missed the { } ... I forgot about that.
Glad that was caught.