0

我正在尝试根据互联网上的教程实现一个表单。不幸的是,我无法让它工作。当我单击“发送”时,页面会重新加载,仅此而已。知道问题是什么吗?非常感谢

HTML:

<div class="block-right">  <h1>Formulaire de contact</h1>
            <!-- CONTACT FORM-->
            <div class="contact-form">


                <form id="contact" method="post" class="clearfix">
                    <div class="clearfix">
                        <input id="name" name="name" placeholder="Name" type="text" value="">
                        <input id="email" name="email" placeholder="Email" type="email" value="">
                    </div>

                    <textarea id="message" name="message" placeholder="Message"></textarea>

                    <input type="submit" value="Envoyer" name="submit">

  <p class="success" style="display:none">Your message has been sent successfully.</p>
  <p class="error" style="display:none">E-mail must be valid and message must be longer than 100 characters.</p>
                </form>
            </div><!-- /.contact-form -->
</div>  <!-- End DIV block-right -->

JS:

    // Contact Form
  $(document).ready(function(){
  $("#contact").submit(function(e){
     e.preventDefault();
      var name = $("#name").val();
      var email = $("#email").val();
      var message = $("#message").val();
      var dataString = 'name=' + name + '&email=' + email + '&message=' + message;
      function isValidEmail(emailAddress) {
        var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
        return pattern.test(emailAddress);
      };

      if (isValidEmail(email) && (message.length > 100) && (name.length > 1)){
        $.ajax({
        type: "POST",
        url: "../sendmessage.php",
        data: dataString,
        success: function(){
          $('.success').fadeIn(1000);
        }
        });
      } else{
        $('.error').fadeIn(1000);
      }

      return false;
    });
});

PHP:

<?php
// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['message']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {

  // detect & prevent header injections
  $test = "/(content-type|bcc:|cc:|to:)/i";
  foreach ( $_POST as $key => $val ) {
    if ( preg_match( $test, $val ) ) {
      exit;
    }
  }

  //send email
  mail( "xyz@gmail.com", "Contact Form: ".$_POST['name'], $_POST['message'], "From:" . $_POST['email'] );

}
?>
4

1 回答 1

0

记得把你的代码包装在里面$(document).ready

$(document).ready(function(){
  $("#contact").submit(function(e){
     e.preventDefault();

     //Your code.

     return false;
  });
});

或使用委托事件:

$(document).on("submit","#contact",function(e){
      e.preventDefault();

         //Your code.

      return false;
 });

更新: 如果您使用.noConflict();放弃对$. 你可以试试:

jQuery(document).ready(function(){
      jQuery("#contact").submit(function(e){
         e.preventDefault();

         //Your code.

         return false;
      });
    });
于 2013-09-28T13:30:51.577 回答