Carrie C Carrie C - 4 months ago 6
HTML Question

Php form is emailing me, but not sending the form contents

I am having trouble getting my form to send me the user filled form data. Below is the HTML for the form and the PHP. The form is being sent to me, but I am not sure if I set up the HTML and PHP correctly. I've tried digging around for answers, but I don't have a lot of knowledge in PHP and most of the other questions have more complex forms. Trying to take what I've learned from looking for the answer, I think I've Frankensteined everything. Please help.

<form method="post" action="contact2.php" enctype="text/plain">
<label for="name">Name</label>
<input name="name" type="text" id="name">
<label for="email">Email Address</label>
<input name="email" type="text" id="email">
<label for="phone">Phone Number</label>
<input name="phone" type="text" id="phone">
<label for="comments">Comments</label>
<textarea name="comments" id="comments" rows="5" cols="38"><?php echo $comment;?></textarea>
<input type="submit" value="Submit">
</form>


<?php
$to = 'myemail@myemail.com';
$subject = "Message from my internet form";
$message .= $_POST['name'];
$message .= $_POST['email'];
$message .= $_POST['phone'];
$message .= $_POST['comments'];
$headers = 'From: ' . $_POST['name'] . $_POST['email'] . "\r\n" .
'Reply-To: notsure@whatgoeshere.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
header('Location: http://mypage.com/thankyou.html');
exit();
?>


edit: updated the above code to reflect my changes. I'm still not getting the form info to my email and the email I receive looks funny, "www@myemailprovider.com" it starts with www and looks like it's coming from me instead of the user filling out the form.

edit2: This works now and all code is updated. For the curious, I was able to format the incoming message by putting a table in my PHP form. It looks like this:

<?php
$to = 'youremail@email.com';
$subject = "Response via your website";
$message .= $_POST['name'];
$message .= $_POST['email'];
$message .= $_POST['phone'];
$message .= $_POST['comments'];
$headers .= "Content-type: text/html charset=iso-8859-1 \r\n";
'From: ' . $_POST['name'] . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = '
<html>
<head>
<title>Some title</title>
</head>
<body>
<table>
<tr><td>Name:</td><td>'.$name.'</td></tr>
<tr><td>Email:</td><td>'.$email.'</td></tr>
<tr><td>Phone:</td><td>'.$phone.'</td></tr>
<tr><td>'.$comments.'</td></tr>
</table>
</body>
</html>';

mail($to, $subject, $message, $headers);
header('Location: http://testpage.com/test.html');
exit();
?>

Answer

POST variables require that a name attribute be included with your inputs.

Try this:

<form  method="post" action="contact2.php" enctype="text/plain">
      <label for="name">Name</label>
      <input name="name" type="text" id="name">
      <label for="email">Email Address</label>
      <input name="email" type="text" id="email">
      <label for="phone">Phone Number</label>
      <input name="phone" type="text" id="phone">
      <label for="comments">Comments</label>
      <textarea name="comments" id="comments" rows="5" cols="38"><?php echo $comment;?></textarea>
<input type="submit" value="Submit">
</form>