我正在尝试创建一个两页表单 - 第一页是可选的。这是一个开始的提示。在我开始添加PHPMailer之前,它一直运行良好。现在,当我在第一页上的第一个可选表单上单击提交时,它会通过邮件程序提交它,并且它的所有验证/错误,然后如果我注释掉错误 if 语句,则正确通过电子邮件发送它。
这是第一个可选的表单。它应该将您带到估算页面,这些值已经存储在表单中,这样您就不必重新做一遍。
<form class="form-horizontal first-form" action="estimate.php" method="post">
<div class="form-group">
<label for="inputMoveDate" class="col-xs-4 text-left">Move Date</label>
<div class="col-xs-8">
<input type="text" name="date" class="form-control" id="datepicker">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="form-group">
<label for="pickUpZip" class="col-xs-4">Pick Up Zip</label>
<div class="col-xs-8">
<input type="text" name="pickUpZip" class="form-control" id="pickUpZip" placeholder="">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="form-group">
<label for="dropOffZip" class="col-xs-4">Drop Off Zip</label>
<div class="col-xs-8">
<input type="text" name="dropOffZip" class="form-control" id="dropOffZip" placeholder="">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="form-group">
<div class="col-xs-12">
<button type="submit" class="btn btn-default center-block">Continue</button>
</div><!--col-sm-offset-2-->
</div><!--form-group-->
</form>
估计.php第二页。文档顶部的 PHP。
session_start();
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$date = $_POST['date'];
$pickUpZip = $_POST['pickUpZip'];
$dropOffZip = $_POST['dropOffZip'];
$dwellingType = $_POST['dwellingType'];
$salutation = $_POST['salutation'];
$firstName = trim(filter_input(INPUT_POST, "first-name", FILTER_SANITIZE_STRING));
$lastName = trim(filter_input(INPUT_POST, "last-name", FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST, "email", FILTER_SANITIZE_SPECIAL_CHARS));
$phone = $_POST['phone'];
$pickUpAddress = $_POST['pickUpAddress'];
$pickUpCity = $_POST['pickUpCity'];
$pickUpState = $_POST['pickUpState'];
$comments = $_POST['comments'];
$emailBody ="From: $salutation $firstName $lastName'n
Phone Number: $phone'n
E-Mail: $email'n
Message:'n $comments
'n 'n
Move Date: $date'n
Pick Up Address: $pickUpAddress'n
Pick Up City: $pickUpCity'n
Pick Up State: $pickUpState'n
Pick Up Zip: $pickUpZip'n
'n
Drop Off Zip: $dropOffZip'n
Dwelling Type: $dwellingType'n";
if($firstName == "" || $email == "" || $lastName == "") {
echo "Please fill in the required forms: first name, last name, and email.";
exit;
}
if ($_POST["address"] != "") {
echo "Bad form input";
exit;
}
require('phpmailer/class.phpmailer.php');
$mail = new PHPMailer;
if (!$mail->ValidateAddress($email)) {
echo 'Invalid email. Try again.';
exit;
}
$mail->setFrom($email, $firstName);
$mail->addAddress('xxxx@xxx.com', 'name here');
$mail->isHTML(false);
$mail->Subject = 'Estimate Request Inquiry From ' . $firstName;
$mail->Body = $emailBody;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
header("location:estimate.php?status=thanks");
}
这是表单HTML的开始和结束,亮点。
<?PHP
if (isset($_GET["status"]) && $_GET["status"] == "thanks") {
echo "Thank you for your estimate request. We will return your inquiry soon!
</div>";
} else {
?>
<form class="form-horizontal first-form" action="estimate.php" method="post" style="margin: 0 45px;">
<label for="date" class="col-xs-4 text-left">Move Date</label>
<div class="col-xs-8">
<input type="text" class="form-control" id="datepicker" name="date" value="<?php echo $_POST["date"]; ?>">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="form-group">
<label for="pickUpZip" class="col-xs-4">Pick Up Zip</label>
<div class="col-xs-8">
<input type="text" name="pickUpZip" class="form-control" id="pickUpZip" value="<?php echo $_POST["pickUpZip"]; ?>">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="form-group">
<label for="dropOffZip" class="col-xs-4">Drop Off Zip</label>
<div class="col-xs-8">
<input type="text" name="dropOffZip" class="form-control" id="dropOffZip" value="<?php echo $_POST["dropOffZip"]; ?>">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="col-xs-12">
<button type="submit" id="submit" name="submit" class="btn btn-default center-block">Submit</button>
</div><!--col-sm-offset-2-->
</div><!--form-group-->
</form>
<?PHP
}
?>
此行每次都尝试发送电子邮件:
if(!$mail->send()) {
如果失败,您的错误消息将得到回显。您应该将整个块嵌套在 if 语句中,该语句检查表单是否从第 2 页提交。您可以给提交按钮一个不同的值并检查(即"提交1"、"提交2")。