mline86
mline86

Reputation: 28

$_POST not working when sending HTML email

I've been figuring out what's wrong with my code. I keep getting $_POST not working. This is my HTML code:

<form id="EmailForm" action="mailForm.php" method="post" 
  onsubmit="validateForm()" enctype="text/plain">
  <div>
    Name: <input type="text" value="" name="name" size="30" />
    Email: <input type="text" value="" name="email" size="30" />
    Subject: <input type="text" value="" name="subject" size="60" />
    Message: <br />
    <textarea name="message" rows="5" cols="60"></textarea>
    <input type="submit" value="Send" />
  </div>
</form>

This is my mailForm.php code:

<?php
$name = $_POST['name']; 
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mailsent = mail("[email protected]", "$subject", "$message\n\n$name\n$email", "");
if ($mailsent) {
echo ("Your email has been sent. Thank you for using our mail form. <br />");
echo ("Name: ".$name."<br />");
echo ("Email: ".$email."<br />");
echo ("Subject: ".$subject."<br />");
echo ("Message: ".$message);
}
?>

So far, I can get emails sent but all emails are empty. The name or email didn't show up, but get changed to the default name from the web hosting service. No name, no email, no subject, no message. At the mailForm.php, I got the following only:

Your email has been sent. Thank you for using our mail form.   
Name:
Email:
Subject: 
Message:

I've tried echo $_POST["name"]; echo $_POST["email"] but got empty page.

Upvotes: 0

Views: 4005

Answers (2)

Night2
Night2

Reputation: 1153

You should not use enctype="text/plain" in this case in your <form> tag ...

php Will not show your form elements in $_POST if you use that code.

php has a variable called $HTTP_RAW_POST_DATA which will store your posted data in it as simple string when using enctype="text/plain".

So your <form> tag in HTML code should be this:

<form id="EmailForm" action="mailForm.php" method="post" onsubmit="validateForm()">

Valid enctype values in <form> tag are:

application/x-www-form-urlencoded: Default. All characters are encoded before sent (spaces are converted to "+" symbols, and special characters are converted to ASCII HEX values)

multipart/form-data: Spaces are converted to "+" symbols, but no special characters are encoded No characters are encoded. This value is required when you are using forms that have a file upload control

text/plain: Spaces are converted to "+" symbols, but no special characters are encoded

Except text/plain the other 2 methods will be available as array in $_POST ...

Upvotes: 1

Mihai Iorga
Mihai Iorga

Reputation: 39704

Just remove the enctype="text/plain":

<form id="EmailForm" action="mailForm.php" method="post" onsubmit="validateForm()">

Valid values in PHP for enctype in <form> tag are:

application/x-www-form-urlencoded
multipart/form-data

Upvotes: 1

Related Questions