HTML表单不是“发布”PHP POST阵列的用户输入值。 我正在尝试通过电子邮件发送表格

问题描述:

Seemingly simple issue: I have an HTML form:

<form action="submit.php" method="post" enctype="text/plain">

            <h3>Owner Information</h3>
            First Name*: <br />
            <input type="text" name="firstname" /><br />
            Last Name*: <br />
            <input type="text" name="lastname" /><br /><br />
            Are you the owner on title?*: <br />
            <input type="radio" name="titleowner" value="yes" />Yes
            <input type="radio" name="titleowner" value="no" />No<br /><br />
</form>

And then here is submit.php:

<?php

$admin_email = "aloha@hi.com";
 $email = "hey@gmail.com";
$subject = "subject";
$message = htmlspecialchars($_POST['firstname'] . " " . $_POST['lastname']);
$message .= "Title Owner? " . htmlspecialchars($_POST["titleowner"]);
$message .= "Mailing Address: " . htmlspecialchars($_POST['mailingaddress']) . "City: " .      htmlspecialchars($_POST['city']) . "State:" . htmlspecialchars($_POST['state']) . "Zip Code" .   (int)$_POST['zipcode'] . "Phone Number:" . strip_tags($_POST['phoneNum']);

//send email
mail($admin_email, $subject, $message);

echo $message;

//Email response
echo "Thank you for contacting us!";
?>

The 'echo message' produces this when the user submits:

Title Owner? Mailing Address: City: State:Zip Code0Phone Number:Thank you for contacting us!

As you can see, the variable for some reason did not populate. Any help is very much appreciated. Thank you.

看似简单的问题:我有一个HTML表单: p>

   &lt; form action =“submit.php”method =“post”enctype =“text / plain”&gt; 
 
&lt; h3&gt;所有者信息&lt; / h3&gt; 
名字*:&lt; br /&gt;  
&lt; input type =“text”name =“firstname”/&gt;&lt; br /&gt; 
姓氏*:&lt; br /&gt; 
&lt; input type =“text”name =“lastname  “/&gt;&lt; br /&gt;&lt; br /&gt; 
您是标题所有者吗?*:&lt; br /&gt; 
&lt; input type =”radio“name =”titleowner“value =  “是”/&gt;是
&lt; input type =“radio”name =“titleowner”value =“no”/&gt;否&lt; br /&gt;&lt; br /&gt; 
&lt; / form&gt; 
   code>  pre> 
 
 

接下来是submit.php: p>

 &lt;?php 
 
 $ admin_email =“aloha  @ hi.com“; 
 $ email =”hey@gmail.com“; 
 $ subject =”subject“; 
 $ message = htmlspecialchars($ _ POST ['firstname']。”“。$ _POST ['  lastname']); 
 $ message。=“标题所有者?”。  htmlspecialchars($ _ POST [“titleowner”]); 
 $ message。=“邮寄地址:”。  htmlspecialchars($ _ POST ['mailingaddress'])。  “城市:”。  htmlspecialchars($ _ POST ['city'])。  “国家:”。  htmlspecialchars($ _ POST ['state'])。  “邮政编码” 。  (int)$ _ POST ['zipcode']。  “电话号码:” 。  strip_tags($ _ POST ['phoneNum']); 
 
 //发送电子邮件
mail($ admin_email,$ subject,$ message); 
 
echo $ message; 
 
 //电子邮件回复
echo“ 感谢您与我们联系!“; 
?&gt; 
  code>  pre> 
 
 

当用户提交时,”echo message“会产生此信息: p>

 标题所有者? 邮寄地址:城市:州:邮政编码0电话号码:感谢您与我们联系!
  code>  pre> 
 
 

如您所见,由于某种原因,变量没有填充。 很感谢任何形式的帮助。 谢谢。 p> div>

This:

enctype="text/plain"

is the culprit. The text/plain encoding isn't reliably machine decodable and PHP won't parse it.

Remove the enctype attribute entirely.

For POST forms you should use the enctype="multipart/form-data". Thats what I'm using for the email forms and it just works.

As Quentin stated in the comments though, the default for a form without file fields is enctype="application/x-www-form-urlencoded", so if you want to state an enctype explicite, use that one.