我正在尝试使用PHP为我的网站制作联系表单。我也使用PHP Mailer通过电子邮件发送消息。按下表单上的“发送”按钮后,页面应重定向到自身,而是转到正确的链接,但不是正确的页面。这是代码:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
if ($name == "" OR $email == "" OR $message == "") {
$error_message = "Oops! Make sure you fill out all of the information!";
}
if (!isset($error_message)) {
foreach( $_POST as $value ){
if( stripos($value,'Content-Type:') !== FALSE ){
$error_message = "There was a problem with the information you entered.";
}
}
}
if (!isset($error_message) && $_POST["address"] != "") {
$error_message = "Your form submission has an error.";
}
require_once("inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
if (!isset($error_message) && !$mail->ValidateAddress($email)){
$error_message = "You must specify a valid email address.";
}
if (!isset($error_message)) {
$email_body = "";
$email_body = $email_body . "Name: " . $name . "<br>";
$email_body = $email_body . "Email: " . $email . "<br>";
$email_body = $email_body . "Message: " . $message;
$mail->SetFrom($email, $name);
$address = "myemail";
$mail->AddAddress($address, "name");
$mail->Subject = "name Contact Form Submission | " . $name;
$mail->MsgHTML($email_body);
if($mail->Send()) {
header("Location: contact.php?status=thanks");
exit;
} else {
$error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
}
}
}
?><?php
$pageTitle = "Contact Us";
$section = "contact";
include('inc/header.php'); ?>
<center>
<div class="contact-form">
<h1 id="text">Contact</h1>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p id="text">Thanks for the email! I’ll be in touch shortly!</p>
<?php } else { ?>
<?php
if (!isset($error_message)) {
echo '<p id="text">We’d love to hear from you! Complete the form to send us an email.</p>';
} else {
echo '<p id="text">' . $error_message . '</p>';
}
?>
<form method="post" action="contact.php">
<table>
<tr>
<th>
<label for="name">Name</label>
</th>
<td>
<input type="text" name="name" id="name" value="<?php if (isset($name)) { echo htmlspecialchars($name); } ?>">
</td>
</tr>
<tr>
<th>
<label for="email">Email</label>
</th>
<td>
<input type="text" name="email" id="email" value="<?php if(isset($email)) { echo htmlspecialchars($email); } ?>">
</td>
</tr>
<tr>
<th>
<label for="message">Message</label>
</th>
<td>
<textarea name="message" id="message"><?php if (isset($message)) { echo htmlspecialchars($message); } ?></textarea>
</td>
</tr>
<tr style="display: none;">
<th>
<label for="address">Address</label>
</th>
<td>
<input type="text" name="address" id="address">
<p>Visitor: Please leave this field blank.</p>
</td>
</tr>
</table>
<input type="submit" value="Send">
</form>
<?php } ?>
</center>
</div>
<?php include('inc/footer.php'); ?>
此问题的链接是http://pokobrosapps.com/contact.php。为什么会这样?
答案 0 :(得分:0)
我收到了联系表格!我想出了如何从这里获取一个使用MAMP的错误日志:http://gilbert.pellegrom.me/enable-php-error-reporting-in-mamp/然后它说PHP Mailer文件不是它应该的位置,所以我修复了它,联系表单现在正在工作!