php-Ajax联系表单问题-未发送电子邮件

抱歉,我是菜鸟,但我正在努力.我已经完成并阅读了所有可以找到的内容,但我从未尝试过.非常感谢您的帮助.表单ID在html和javascript中匹配. PHP是用Javascript链接的,而我的html头中是javascript的链接.我想念什么?我也尝试过在网上找到的其他代码,也没有尝试.问题是没有电子邮件发送过.如果您点击发送页面重新加载而已.

        <!--[if lte IE 8]><script src="js/html5shiv.js"></script><![endif]-->
        <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
            <script src="js/jquery.min.js"></script>
    <script src="js/jquery.dropotron.js"></script>
    <script src="js/skel.min.js"></script>
    <script src="js/skel-panels.min.js"></script>
    <script src="js/init.js"></script>
    <script src="js/contact.js"></script>
    <noscript>
        <link rel="stylesheet" href="css/skel-noscript.css" />
        <link rel="stylesheet" href="css/style.css" />
        <link rel="stylesheet" href="css/style-noscript.css" />
    </noscript>


<!-- Contact Form-->
            <div class="content style4 featured">
            <div class="container small">
                <form id="contact" form method="post">
                    <div class="row half">
                        <div class="6u"><input type="text" class="text" name="name" placeholder="Name" /></div>
                        <div class="6u"><input type="text" class="text" placeholder="Email" name="email" /></div>
                    </div>
                    <div class="row half">
                        <div class="12u"><textarea name="text" placeholder="Message"></textarea></div>
                    </div>
                    <div class="row">
                        <div class="12u">
                            <ul class="actions">
                                <li><input type="submit" class="button" value="Send Message" /></li>
                                <li><input type="reset" class="button alt" value="Clear Form" /></li>
<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>
                            </ul>
                        </div>
                    </div>
                </form>
            </div>
        </div>

PHP

<?php
// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['text']) &&      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( "test@gmail.com", "Contact Form: ".$_POST['name'], $_POST['text'], "From:" .      $_POST['email'] );

}
?>

JS

     $('#contact').submit(function() {
    var name = $("#name").val();
    var email = $("#email").val();
    var message = $("#message").val();
 var dataString = 'name=' + name + '&email=' + email + '&message=' + message;
    $.ajax({
        type : "POST",
        url : "mail.php",
        data : dataString,
        cache : false,
        success : function() {              
                $("#contact").fadeOut(300);
                                    $("#notice").fadeIn(400);
        }
    });
   return false;
 });

谢谢.

解决方法:

也许您的js代码在加载jQuery之前运行.尝试在其他js文件之前包含jQuery:

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="js/jquery.dropotron.js"></script>
<script src="js/skel.min.js"></script>
<script src="js/skel-panels.min.js"></script>
<script src="js/init.js"></script>
<script src="js/contact.js"></script>

顺便说一句,您只需要包含一次jQuery,而不是两次.

上一篇:DOM和IE中的 事件对象


下一篇:解决tomcat占用8080端口问题图文教程