当表单具有 enctype="text/plain" 时无法从 $_POST 数组获取字段
Cannot get field from $_POST array when form has enctype="text/plain"
我相信我的代码是正确的,当我删除 isset 语句时它 returns 一个错误,就好像没有设置字段一样。我听说这可能是由于 PHP 在服务器上的配置方式所致,可能是这种情况。变量似乎没有从 HTML 传递到 PHP 代码。如果这意味着什么,我在 windows 服务器 2016
上安装了 PHP WebPlatformInstaller 和 运行 IIS
<?php
echo "test";
if(isset($_POST['email'])) {
echo "success";
$email_to = "temp@gmail.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('You must enter your email and a message');
}
$email_from = $_POST['email']; // required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(strlen($message) < 2) {
$error_message .= 'The message you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
echo "Thank you for reaching out! I'll be in touch.";
<?php
}
?>`
HTML
<form action="contact.php" method="post" enctype="text/plain">
<input type="email" placeholder="Email" name='email' required>
<input class="btn" type='submit' tabindex="1" value='Send'>
<textarea type='text' name='message' required></textarea>
</form>
为什么使用 enctype="text/plain" ??
HTML表单提供三种编码方式:
application/x-www-form-urlencoded (the default)
multipart/form-data
text/plain (Never use)
当您编写客户端代码时,您只需要知道当您的表单包含任何 <input type="">
元素时使用 multipart/form-data。
读这个:Bug #33741 $_POST superglobal not populated
这是关于你的问题。
并在那里回答:
Valid values for enctype in tag are:
application/x-www-form-urlencoded
multipart/form-data
使用代码示例查看这些屏幕截图以查看结果:
text/plain
application/x-www-form-urlencoded
额外:php://输入流
作为请求正文,它除了 @
符号外什么都没有改变(这就是为什么它是 urlencoded)
结果:这不是 PHP 的错误,它是所有 PHP 版本接受支持的 enctypes 的共同特征,它只是不填充 $_POST
数组从 php://input
到 "see" 不同的内容类型。但是如果你坚持你可以从 php://input
流中读取它。
我相信我的代码是正确的,当我删除 isset 语句时它 returns 一个错误,就好像没有设置字段一样。我听说这可能是由于 PHP 在服务器上的配置方式所致,可能是这种情况。变量似乎没有从 HTML 传递到 PHP 代码。如果这意味着什么,我在 windows 服务器 2016
上安装了 PHP WebPlatformInstaller 和 运行 IIS<?php
echo "test";
if(isset($_POST['email'])) {
echo "success";
$email_to = "temp@gmail.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('You must enter your email and a message');
}
$email_from = $_POST['email']; // required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(strlen($message) < 2) {
$error_message .= 'The message you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
echo "Thank you for reaching out! I'll be in touch.";
<?php
}
?>`
HTML
<form action="contact.php" method="post" enctype="text/plain">
<input type="email" placeholder="Email" name='email' required>
<input class="btn" type='submit' tabindex="1" value='Send'>
<textarea type='text' name='message' required></textarea>
</form>
为什么使用 enctype="text/plain" ??
HTML表单提供三种编码方式:
application/x-www-form-urlencoded (the default)
multipart/form-data
text/plain (Never use)
当您编写客户端代码时,您只需要知道当您的表单包含任何 <input type="">
元素时使用 multipart/form-data。
读这个:Bug #33741 $_POST superglobal not populated 这是关于你的问题。
并在那里回答:
Valid values for enctype in tag are:
application/x-www-form-urlencoded
multipart/form-data
使用代码示例查看这些屏幕截图以查看结果:
text/plain
application/x-www-form-urlencoded
额外:php://输入流
作为请求正文,它除了 @
符号外什么都没有改变(这就是为什么它是 urlencoded)
结果:这不是 PHP 的错误,它是所有 PHP 版本接受支持的 enctypes 的共同特征,它只是不填充 $_POST
数组从 php://input
到 "see" 不同的内容类型。但是如果你坚持你可以从 php://input
流中读取它。