btr*_*ror 0 html php forms isset
我正在为我的网站制作一个联系表格,该表格使用 php 将输入的信息发送到我的电子邮件。
我的问题是isset php 函数返回false。我不确定这是为什么。
这是我的php代码(输出结果是“未发送”)
<?php
if (isset($_POST['submit'])){
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "btrorapps@gmail.com";
$headers = "From: " .$mailFrom;
$txt = "You have received an email from " .$name.".\n\n".$message;
if (mail($mailTo, $subject, $txt, $headers)){
echo "<h1>sent successfully</h1>";
} else {
echo "<h1>something went wrong</h1>";
}
} else {
echo "not sent";
}
?>
Run Code Online (Sandbox Code Playgroud)
这是我的表单代码
<form action="contactform.php" method="post" enctype="text/plain">
<label for="name">Name</label>
<input type="text" name="name" placeholder="Full name.." maxlength="20" required>
<label for="mail">Email</label>
<input type="text" name="mail" placeholder="Your email.."
pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z{2,}$" required>
<label for="subject">Subject</label>
<input type="text" name="subject" maxlength="25" placeholder="Subject..">
<label for="message">Message</label>
<input type="text" name="message" placeholder="Message.." style="height:170px"
maxlength="150" required>
<input type="submit" value="submit" name="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
isset功能没有任何问题。它甚至不是与 PHP 相关的问题。
这都是关于表单提交编码的。表单有 3 种有效的编码类型:
您正在使用第三个,其记录如下:
使用该
text/plain格式的有效负载旨在使人类可读。它们不能被计算机可靠地解释,因为格式不明确(例如,无法区分值中的文字换行符和值末尾的换行符)。
您没有得到任何POST变量,因为 PHP$_POST在使用enctype="text/plain".
在$_POSTsuperglobal的文档中:
在请求中使用
application/x-www-form-urlencoded或multipart/form-data作为 HTTP Content-Type时,通过 HTTP POST 方法传递给当前脚本的关联变量数组。
话虽如此,您仍然可以从请求正文中读取原始数据。
$data = file_get_contents('php://input');
Run Code Online (Sandbox Code Playgroud)
但是您将获得整个数据作为字符串(纯文本)。
最后但并非最不重要的,要小心,php://input不可用enctype="multipart/form-data"。