douchen9855 2015-10-08 17:36
浏览 34

Bootstrap联系表单模块问题

I am having trouble with getting this code to not show a thank you modul for incomplete submissions. I also would like the page redirect to originating page(with a pop up modul for either confirmation or error on the page in a modul would be optimal) or just redirect to the originating page without having to create a new PHP file for every page a contact form is on. I have several.

The way it is now it can send mail but it directs to a blank white page with just type on it, and a request to use the browser back button to return to originating page. Also it fires the (Thank you)modul even if in put is in correct. would it please be possible to get help with this?

<form class="o-form" id="contactForm" action="php/contact.php" method="post">
                          <input type="email" name="senderEmail" id="senderEmail" required="required" placeholder="email">
                           <textarea name="message" id="message" required="required placeholder="message"></textarea>
                          <input type="submit" value="send" class="send-button">
                      </form>
                  </div>
              </div>
          </div>
      </div>
      <div class="copyright">
         <span>
           anthonygallina
         </span>
      </div>
   </footer>
   <div class="th-popup">
       <div class="massage-th">
           <h1>Thank You!</h1>
           <p>We apreciate your visit to our home page. We will contact you soon!</p>
       </div>
   </div>

<script src="js/jquery-1.11.1.min.js"></script>
<script src="js/all.js"></script>
<script src="js/jquery.mixitup.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/idangerous.swiper.min.js"></script>
</body>
</html>

<?php

And the other part

// Define some constants
define( "RECIPIENT_NAME", "YOURNAME" );
define( "RECIPIENT_EMAIL", "YOUR@EMAIL.net" );
define( "EMAIL_SUBJECT", "Visitor Message" );

// Read the form values
$success = false;
$senderEmail = isset( $_POST['senderEmail'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderEmail'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";

// If all values exist, send the email
if ( $senderEmail && $message ) {
  $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
  $headers = "From: " . $senderEmail . " <" . $senderEmail . ">";
  $success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );
}

// Return an appropriate response to the browser
if ( isset($_GET["ajax"]) ) {
  echo $success ? "success" : "error";
} else {
?>
<html>
  <head>
    <title>Thanks!</title>
  </head>
  <body>
  <?php if ( $success ) echo "<p>Thanks for sending your message! We'll get back to you soon.</p>" ?>
  <?php if ( !$success ) echo "<p>There was a problem sending your message. Please try again.</p>" ?>
  <p>Click your browser's Back button to return to the page.</p>
  </body>
</html>
<?php
}
?>
  • 写回答

1条回答 默认 最新

  • dongxing7530 2015-10-08 18:17
    关注

    You just need to play with $success like this:

    <?php
    // Define some constants
    define("RECIPIENT_NAME", "YOURNAME");
    define("RECIPIENT_EMAIL", "YOUR@EMAIL.net");
    define("EMAIL_SUBJECT", "Visitor Message");
    
    // Read the form values
    $success = false;
    $senderEmail = isset($_POST['senderEmail']) ? preg_replace("/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderEmail']) : "";
    $message = isset($_POST['message']) ? preg_replace("/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message']) : "";
    
    // If all values exist, send the email
    if ($senderEmail && $message) {
        $formSubmitted = true;
        $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
        $headers = "From: " . $senderEmail . " <" . $senderEmail . ">";
        $success = mail($recipient, EMAIL_SUBJECT, $message, $headers);
    }
    
    // Return an appropriate response to the browser
    if (isset($_GET["ajax"])) {
        echo $success ? "success" : "error";
    } ?>
    <!--Your contact.php page-->
    
    <form class="o-form" id="contactForm" action="php/contact.php" method="post">
        <input type="email" name="senderEmail" id="senderEmail" required="required" placeholder="email">
        <textarea name="message" id="message" required="required placeholder=" message"></textarea>
        <input type="submit" value="send" class="send-button">
    </form>
    </div>
    </div>
    </div>
    </div>
    <div class="copyright">
             <span>
               anthonygallina
             </span>
    </div>
    </footer>
    <div class="th-popup">
        <div class="massage-th">
            <h1>Thank You!</h1>
    
            <p>We apreciate your visit to our home page. We will contact you soon!</p>
        </div>
    </div>
    
    <script src="js/jquery-1.11.1.min.js"></script>
    <script src="js/all.js"></script>
    <script src="js/jquery.mixitup.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/idangerous.swiper.min.js"></script>
    <script>
    
        <?php
         if($formSubmitted)
         {
             if ($success)
             {
                $msg="<p>Thanks for sending your message! We'll get back to you soon.</p>";
             }
             else
             {
                $msg="<p>There was a problem sending your message. Please try again.</p>";
             }
         //Open your confirmation or error model here using $msg
         }
         ?>
    </script>
    </body>
    </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?