【问题标题】:Two Page Form Submits On First Page两页表单在第一页提交
【发布时间】:2016-04-25 20:25:15
【问题描述】:

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

【问题讨论】:

  • 您只是在第二页检查if ($_SERVER["REQUEST_METHOD"]=="POST") {,并且您是从第一页发布的,因此它认为您正在提交最终提交,有几种方法可以解决这个问题,一种是在第一个表单上添加一个隐藏字段,然后在运行验证代码之前检查以确保第二个页面上不存在该字段。
  • 这对我来说很有意义。我试图通过使用 if 语句使其工作“如果此隐藏元素保持空白,则执行所有这些操作。基本上在没有空白的情况下将所有其他 if 语句包装起来给出错误并退出。到目前为止它不起作用。任何建议?

标签: php html forms twitter-bootstrap phpmailer


【解决方案1】:

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

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

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

【讨论】:

  • 什么样的if语句?对不起,我是一个真正的初学者。问题是,第一个表单完全是可选的,不是必需的,所以我正在努力解决如何继续传递这些值,如果它们在那里,但如果没有,请等待第二个表单的提交。
猜你喜欢
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-29
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 2016-04-05
相关资源
最近更新 更多