第一页提交两页表格

时间:2016-04-25 20:25:15

标签: php html forms twitter-bootstrap phpmailer

我正在尝试创建一个双页表单 - 第一页是可选的。它是一个开始的提示。在我开始添加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('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
                                }
                            ?>

1 个答案:

答案 0 :(得分:0)

此行尝试每次都发送电子邮件:

if(!$mail->send()) {

如果失败,则会回显您的错误消息。 您应该将整个块嵌套在if语句中,该语句检查表单是否是从第2页提交的。您可以为提交按钮指定不同的值并检查它(即&#39; submit1&#39;,&#39; submit2&# 39。)