PHP isset 函数有问题
PHP Issue with isset function
我正在为我的网站制作联系表格,使用 php 将输入的信息发送到我的电子邮件。
我的问题是 isset php 函数 returns 错误。我不确定这是为什么。
这是我的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";
}
?>
这是我的表单代码
<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>
不要使用引用提交按钮的 isset($_POST['submit'])
,以防表单上有多个提交按钮,因为按钮并不总是被按下。这在过去给我带来了问题。
在任何字段上按 Enter 键将提交表单,第一个提交按钮可用。
使用其中一个字段。在您的情况下,您可以使用 if (isset($_POST['name'])){
isset
函数没有问题。它甚至不是 PHP 相关的问题。
都是关于表单提交编码的。表单有 3 种有效的编码类型:
您正在使用第三个,其记录如下:
Payloads using the text/plain
format are intended to be human
readable. They are not reliably interpretable by computer, as the
format is ambiguous (for example, there is no way to distinguish a
literal newline in a value from the newline at the end of the value).
您没有获得任何 POST
变量,因为在使用 enctype="text/plain"
时 PHP 不会填充 $_POST
。
在 $_POST
superglobal 的文档中:
An associative array of variables passed to the current script via the
HTTP POST method when using application/x-www-form-urlencoded
or
multipart/form-data
as the HTTP Content-Type in the request.
话虽如此,您仍然可以从请求正文中读取原始数据。
$data = file_get_contents('php://input');
但是您将获得整个数据作为 字符串(纯文本)。
最后但同样重要的是,请注意,php://input
不适用于 enctype="multipart/form-data"
。
我正在为我的网站制作联系表格,使用 php 将输入的信息发送到我的电子邮件。
我的问题是 isset php 函数 returns 错误。我不确定这是为什么。
这是我的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";
}
?>
这是我的表单代码
<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>
不要使用引用提交按钮的 isset($_POST['submit'])
,以防表单上有多个提交按钮,因为按钮并不总是被按下。这在过去给我带来了问题。
在任何字段上按 Enter 键将提交表单,第一个提交按钮可用。
使用其中一个字段。在您的情况下,您可以使用 if (isset($_POST['name'])){
isset
函数没有问题。它甚至不是 PHP 相关的问题。
都是关于表单提交编码的。表单有 3 种有效的编码类型:
您正在使用第三个,其记录如下:
Payloads using the
text/plain
format are intended to be human readable. They are not reliably interpretable by computer, as the format is ambiguous (for example, there is no way to distinguish a literal newline in a value from the newline at the end of the value).
您没有获得任何 POST
变量,因为在使用 enctype="text/plain"
时 PHP 不会填充 $_POST
。
在 $_POST
superglobal 的文档中:
An associative array of variables passed to the current script via the HTTP POST method when using
application/x-www-form-urlencoded
ormultipart/form-data
as the HTTP Content-Type in the request.
话虽如此,您仍然可以从请求正文中读取原始数据。
$data = file_get_contents('php://input');
但是您将获得整个数据作为 字符串(纯文本)。
最后但同样重要的是,请注意,php://input
不适用于 enctype="multipart/form-data"
。