联系表单不适用于新的jQuery版本(3.3.1)

时间:2018-02-04 08:16:48

标签: php html forms email

我错了,我错过了PHP文件中的任何内容。使联系表单工作的唯一方法是,如果我使用旧版本的jQuery,但情况并非如此,因为我需要在我的网站上使用SSL,并且Google确定网站不安全,如果它们较旧则需要加载不安全的脚本。

以下是旧版本的脚本:

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

window.jQuery ||文件撰写(&#39;&LT; /脚本&GT;&#39)

   <script type="text/javascript" src="js/jquery-migrate-1.2.1.min.js"></script>

这就是我试图使用的内容。我下载了js文件并上传到服务器,但我认为由于许多功能更改,我的PHP文件不再能够完成工作了。

有人可以帮我更新我的PHP文件,因为我没有真正编码:o。试图理解它是如何工作的,但我感到困惑。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

   <script>window.jQuery || document.write('<script src="js/jquery-3.3.1.min.js"><\/script>')</script>

   <script type="text/javascript" src="js/jquery-migrate-3.0.0.min.js"></script>

我使用Styleshout中名为Kreative101的模板并以多种方式对其进行了修改,但我没有触及联系表单和PHP文件(仅插入错误报告和电子邮件地址)。

我不知道它是否需要任何jQuery脚本才能工作,因为我确实改变了它并且Modal Popup停止工作。现在它回到了原始版本(至少是带脚本的页脚)。该模式适用于联系表格。

任何帮助都将受到高度赞赏;)。

If a reload the page says the action I took will be repeated

之前联系表格有效,我测试过了。

这是HTML代码

<section id="contact">

      <div class="row section-head">
         <div class="col full">
            <h2>Contact</h2>
            <p class="desc">Get in touch with us</p>
         </div>
      </div>

      <div class="row">

         <div class="col g-7">

            <!-- form -->
            <form name="contactForm" id="contactForm" method="post" action="">
                    <fieldset>

                  <div>
                           <label for="contactName">Name <span class="required">*</span></label>
                           <input name="contactName" type="text" id="contactName" size="35" value="" />
                  </div>

                  <div>
                           <label for="contactEmail">Email <span class="required">*</span></label>
                           <input name="contactEmail" type="text" id="contactEmail" size="35" value="" />
                  </div>

                  <div>
                           <label for="contactSubject">Subject</label>
                           <input name="contactSubject" type="text" id="contactSubject" size="35" value="" />
                  </div>

                  <div>
                     <label  for="contactMessage">Message <span class="required">*</span></label>
                     <textarea name="contactMessage"  id="contactMessage" rows="15" cols="50" ></textarea>
                  </div>

                  <div>
                     <button class="submit">Submit</button>
                     <span id="image-loader">
                        <img src="images/loader.gif" alt="" />
                     </span>
                  </div>

                    </fieldset>
                </form> <!-- Form End -->

            <!-- contact-warning -->
            <div id="message-warning"></div>
            <!-- contact-success -->
                <div id="message-success">
               <i class="icon-ok"></i>Your message was sent, thank you!<br />
                </div>

         </div>

PHP代码(我插入了错误报告,但我不知道它是否正确。)

<?php

error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");

// Replace this with your own email address
$siteOwnersEmail = 'info@virtualpropertyreview.com';


if($_POST) {

   $name = trim(stripslashes($_POST['contactName']));
   $email = trim(stripslashes($_POST['contactEmail']));
   $subject = trim(stripslashes($_POST['contactSubject']));
   $contact_message = trim(stripslashes($_POST['contactMessage']));

   // Check Name
    if (strlen($name) < 2) {
        $error['name'] = "Please enter your name.";
    }
    // Check Email
    if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
        $error['email'] = "Please enter a valid email address.";
    }
    // Check Message
    if (strlen($contact_message) < 15) {
        $error['message'] = "Please enter your message. It should have at least 15 characters.";
    }
   // Subject
    if ($subject == '') { $subject = "Contact Form Submission"; }


   // Set Message
   $message .= "Email from: " . $name . "<br />";
    $message .= "Email address: " . $email . "<br />";
   $message .= "Message: <br />";
   $message .= $contact_message;
   $message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";

   // Set From: header
   $from =  $name . " <" . $email . ">";

   // Email Headers
    $headers = "From: " . $from . "\r\n";
    $headers .= "Reply-To: ". $email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


   if (!$error) {

      ini_set("sendmail_from", $siteOwnersEmail); // for windows server
      $mail = mail($siteOwnersEmail, $subject, $message, $headers);

        if ($mail) { echo "OK"; }
      else { echo "Something went wrong. Please try again."; }

    } # end if - no validation error

    else {

        $response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
        $response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
        $response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;

        echo $response;

    } # end if - there was a validation error

}

?>

1 个答案:

答案 0 :(得分:0)

在评论中我建议在发送邮件后重定向 - 不确定你是否理解我的意思但是就像这样。

$mail = mail( $siteOwnersEmail, $subject, $message, $headers );
header('Location: ?mailsent=' . $mail ? 'true' : 'error' );

如果页面意外重新加载等,应该阻止提交表单

您可以使用该GET变量显示一条消息,以报告邮件发送的状态。

if( !empty( $_GET['mailsent'] ) ){
    echo $_GET['mailsent']=='true' ? "your message was sent" : "Sorry, there was an error"; /* etc */
}