表单提交后确认消息不显示- jQuery/Ajax


Confirmation message not showing after form submission - jQuery/Ajax

我在这里使用教程:http://designwoop.com/2012/07/tutorial-coding-a-jquery-popup-modal-contact-form/和表单正确发送数据。但是,在单击submit之后,表单只显示"发送"消息,而不显示确认消息。当我在本地和开发环境上进行测试时,就会发生这种情况。我在控制台中没有看到任何错误信息。

这是我第一次使用ajax,我是jQuery/JavaScript的新手。不知道这里有什么问题。它似乎没有把数据解读为真实的。我将True更改为False,看看会发生什么,但仍然不起作用。有人能帮忙吗?

仅供参考,我在一个已经调用1.3.2 jQuery库并且不能删除引用的网站上实现了这一点,所以我必须使用noConflict。当我只引用旧的库时,这不起作用。

我的目标是创建一个模态,弹出当用户进入网站,但不是如果cookie设置或如果已经有一个特定的div在他们进入的页面。

脚本如下:

          <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
          <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
          <script type="text/javascript" src="/wcsstore/CVWEB/images/home/js/jquery.fancybox.js?v=2.0.6"></script> 
    <script type="text/javascript">
    var jQuery_1_7_2 = jQuery.noConflict(true);
    function validateEmail(email) { 
        var reg = /^(([^<>()[']''.,;:'s@'"]+('.[^<>()[']''.,;:'s@'"]+)*)|('".+'"))@(('[[0-9]{1,3}'.[0-9]{1,3}'.[0-9]{1,3}'.[0-9]{1,3}'])|(([a-zA-Z'-0-9]+'.)+[a-zA-Z]{2,}))$/;
        return reg.test(email);
    }
    jQuery_1_7_2(document).ready(function(){
            var emailFormExists = jQuery_1_7_2('#e2ma_signup_form');
            if (document.cookie.indexOf('visited=true') == -1 && !(emailFormExists.length)){
                var fifteenDays = 1000*60*60*24*15;
                var expires = new Date((new Date()).valueOf() + fifteenDays);
                document.cookie = "visited=true;expires=" + expires.toUTCString();
                jQuery_1_7_2.fancybox({width:"100%", inline:true, href:"#inline"});
            }
            else
            {
            jQuery_1_7_2('#e2ma_signup_form').length
                var fifteenDays = 1000*60*60*24*15;
                var expires = new Date((new Date()).valueOf() + fifteenDays);
                document.cookie = "visited=true;expires=" + expires.toUTCString();  
            }
        jQuery_1_7_2("#contact").submit(function() { return false; });

    jQuery_1_7_2("#send").on("click", function(){
        var emailval  = jQuery_1_7_2("#email").val();
        var mailvalid = validateEmail(emailval);
        if(mailvalid == false) {
            jQuery_1_7_2("#email").addClass("error");
        }
        else if(mailvalid == true){
            jQuery_1_7_2("#email").removeClass("error");
        }
        if(mailvalid == true) {
            // if both validate we attempt to send the e-mail
            // first we hide the submit btn so the user doesnt click twice
            jQuery_1_7_2("#send").replaceWith("<em>sending...</em>");
            jQuery_1_7_2.ajax({
                type: 'POST',
                url: 'http://lcoawebservices.com/scripts/email-opt-in.php',
                data: jQuery_1_7_2("#contact").serialize(),
                success: function(data) {
                    if(data == "true") {
                        jQuery_1_7_2("#contact").fadeOut("fast", function(){
                            jQuery_1_7_2(this).before("<p><strong>Thanks for opting in!</strong></p>");
                            setTimeout("jQuery_1_7_2.fancybox.close()", 1000);
                        });
                    }
                }
            });
        }
    }); 
                });
            </script>

,这里是HTML:

            <div id="inline">
                <h2>Join the our mailing list!</h2>
                <form id="contact" name="contact" action="#" method="post">
                    <label for="email">Your E-mail</label>
                    <input type="email" id="email" name="email" class="txt">
                    <button id="send">Send E-mail</button>
                </form>
            </div>  

这里是PHP(我也是新手)

        <?php
        $sendto   = "llantz@lecreuset.com";
        $usermail = $_POST['email'];
        $content  = nl2br($_POST['msg']);
        $subject  = "New Feedback Message";
        $headers  = "From: " . strip_tags($usermail) . "'r'n";
        $headers .= "Reply-To: ". strip_tags($usermail) . "'r'n";
        $headers .= "MIME-Version: 1.0'r'n";
        $headers .= "Content-Type: text/html;charset=utf-8 'r'n";
        $msg  = "<html><body style='font-family:Arial,sans-serif;'>";
        $msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>New User Feedback</h2>'r'n";
        $msg .= "<p><strong>Sent by:</strong> ".$usermail."</p>'r'n";
        $msg .= "<p><strong>Message:</strong> ".$content."</p>'r'n";
        $msg .= "</body></html>";

        if(@mail($sendto, $subject, $msg, $headers)) {
            echo "true";
        } else {
            echo "false";
        }
        ?>

我弄清楚了-这是由于跨域访问问题。我还没有关于如何解决它的答案,但想张贴这个,所以没有人花时间看我的代码,试图修复它。