我相信我的代码是正确的,当我删除isset语句时,它将返回错误,就像未设置字段一样。我听说这可能是由于在服务器上如何配置PHP导致的。似乎变量没有从HTML传递到PHP代码。如果有什么需要,我已经在WebPlatformInstaller中安装了PHP,并在Windows Server 2016上运行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>
答案 0 :(得分:3)
阅读:Bug #33741 $_POST superglobal not populated 这与您的问题有关。
并在此处回答
标记中的enctype的有效值为:
application / x-www-form-urlencoded
multipart / form-data
使用代码示例检查这些屏幕截图以查看结果:
文本/纯文本
应用程序/ x-www-form-urlencoded
额外: php://输入流
作为请求正文,它仅更改@
符号(这就是 urlencoded 的原因)
结果:这不是PHP的错误,它是所有PHP版本都接受支持的enctypes的通用功能,只是当它“看到”不同时,不会从$_POST
填充php://input
数组内容类型。但是,如果您坚持可以从php://input
流中读取它。
答案 1 :(得分:0)
为什么要使用enctype =“ text / plain” ??
HTML表单提供了三种编码方法:
application/x-www-form-urlencoded (the default)
multipart/form-data
text/plain (Never use)
编写客户端代码时,您需要知道的是,当表单包含任何<input type="">
元素时,使用multipart / form-data。