PHP 联系表失败
PHP Contact Form Failing
我想知道这个联系表出了什么问题。我不确定为什么,但它一直默认为 else
并说 Something went wrong, please try again later.
我不知道为什么,而 error_log 没有显示任何内容。有什么明显的我想念的吗?我是 PHP.
的新手
PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$captcha = $_POST['captcha'];
$from = 'From: HankSmith.com Contact Form';
$to = 'thatbraxjohnsonguy@gmail.com';
$subject = 'HANK SMITH CONTACT FORM';
$body = "
From: $name\n
E-Mail: $email\n
Phone: $phone\n
Message: $message\n";
if ($_POST['submit'] and $captcha == 4) {
if (mail ($to, $subject, $body, $from)) {
echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click <a href="../index.php">here</a> to return to the website.</p>';
} else {
echo '<p>Something went wrong, try again later!</p>';
}
}
?>
HTML表格
<form class="contactform" method="post" action="php/contact.php">
<h3>Name</h3>
<input class="form inputboxes" type="text" name="name">
<h3>Email</h3>
<input class="form inputboxes" type="text" name="email">
<h3>Phone</h3>
<input class="form inputboxes" type="text" name="phone">
<h3>Message</h3>
<textarea class="form inputboxes" name="message"></textarea>
<h3 class="captchastyle">Are you real? What is 2 + 2?</h3><input class="captcha captchastyle" type="text" name="captcha" maxlength="1">
<input class="form submit" name="submit" type="submit" value="Submit">
</form>
根据您的情况使用isset
:
if (isset($_POST['submit']) && $captcha == 4)
检查字段是否为空以及表单是否已提交。
而且 &&
比 and
更好看这里的原因:
当我将它复制到我的文本编辑器中时,它抛出了一个解析错误,因为 if (mail)... 部分中的 echo 语句没有右括号。我从不使用'and',我通常使用'&&',但实际上在这种情况下似乎并不重要。
if ($_POST['submit'] && $captcha == 4) {
if (mail ($to, $subject, $body, $from)) {
echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click <a href="../index.php">here</a> to return to the website.</p>';
} // THIS MUST BE HERE
} else {
echo '<p>Something went wrong, try again later!</p>';
}
此外,您可能需要一个 'else' 条件来表示 if (mail)... 语句。如果您看到空白页,那是因为您没有处理 mail() 函数 returns false:
时发生的情况
if (mail ($to, $subject, $body, $from)) {
echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click <a href="../index.php">here</a> to return to the website.</p>';
} else {
echo '<p>Problem sending mail!';
}
老实说,原生 PHP mail() 函数很烂!考虑改用 SwiftMailer or PHPMailer。
我想知道这个联系表出了什么问题。我不确定为什么,但它一直默认为 else
并说 Something went wrong, please try again later.
我不知道为什么,而 error_log 没有显示任何内容。有什么明显的我想念的吗?我是 PHP.
PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$captcha = $_POST['captcha'];
$from = 'From: HankSmith.com Contact Form';
$to = 'thatbraxjohnsonguy@gmail.com';
$subject = 'HANK SMITH CONTACT FORM';
$body = "
From: $name\n
E-Mail: $email\n
Phone: $phone\n
Message: $message\n";
if ($_POST['submit'] and $captcha == 4) {
if (mail ($to, $subject, $body, $from)) {
echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click <a href="../index.php">here</a> to return to the website.</p>';
} else {
echo '<p>Something went wrong, try again later!</p>';
}
}
?>
HTML表格
<form class="contactform" method="post" action="php/contact.php">
<h3>Name</h3>
<input class="form inputboxes" type="text" name="name">
<h3>Email</h3>
<input class="form inputboxes" type="text" name="email">
<h3>Phone</h3>
<input class="form inputboxes" type="text" name="phone">
<h3>Message</h3>
<textarea class="form inputboxes" name="message"></textarea>
<h3 class="captchastyle">Are you real? What is 2 + 2?</h3><input class="captcha captchastyle" type="text" name="captcha" maxlength="1">
<input class="form submit" name="submit" type="submit" value="Submit">
</form>
根据您的情况使用isset
:
if (isset($_POST['submit']) && $captcha == 4)
检查字段是否为空以及表单是否已提交。
而且 &&
比 and
更好看这里的原因:
当我将它复制到我的文本编辑器中时,它抛出了一个解析错误,因为 if (mail)... 部分中的 echo 语句没有右括号。我从不使用'and',我通常使用'&&',但实际上在这种情况下似乎并不重要。
if ($_POST['submit'] && $captcha == 4) {
if (mail ($to, $subject, $body, $from)) {
echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click <a href="../index.php">here</a> to return to the website.</p>';
} // THIS MUST BE HERE
} else {
echo '<p>Something went wrong, try again later!</p>';
}
此外,您可能需要一个 'else' 条件来表示 if (mail)... 语句。如果您看到空白页,那是因为您没有处理 mail() 函数 returns false:
时发生的情况if (mail ($to, $subject, $body, $from)) {
echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click <a href="../index.php">here</a> to return to the website.</p>';
} else {
echo '<p>Problem sending mail!';
}
老实说,原生 PHP mail() 函数很烂!考虑改用 SwiftMailer or PHPMailer。