我想創建一個雙頁表單 - 第一頁是可選的。它是一個提示開始。直到我開始添加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>
Estimate.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('[email protected]', '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($ _SERVER [「REQUEST_METHOD」] ==「POST」){',並且你從第一頁發佈,所以它認爲你是最終提交的,有幾種方法可以解決這個問題,一種方法是在第一個表單上添加一個隱藏字段,然後在運行驗證代碼之前檢查並確保第二頁上不存在。 – cmorrissey
這對我有意義。我試圖通過使用if語句來實現它「如果這個隱藏的元素仍然是空白的,那麼做所有這些事情。基本上包裝其他所有這些if語句,而不是空白給出錯誤並退出。 ? –