简体   繁体   中英

Confirmation message not showing after form submission - jQuery/Ajax

I'm working with the tutorial here: http://designwoop.com/2012/07/tutorial-coding-a-jquery-popup-modal-contact-form/ and the form sending the data correctly. However, after clicking submit, the form just shows the "sending" message rather than showing the confirmation message. This happens when I test locally and on my development environment. I don't see any error messages in my console.

This is my first time working with ajax and I am a newbie with jQuery/JavaScript. Not sure what the problem is here. It seems that it is not reading the data as true. I changed True to False to see what would happen and it still doesn't work. Can anyone help, please?

FYI I am implementing this on a site that already calls the 1.3.2 jQuery library and can not remove the reference, so I have to use noConflict. This doesn't work when I reference only the older library.

My goal is to create a modal that pops up when a user enters the website, but not if a cookie is set or if there is already a specific div on the page they enter through.

Here are the scripts:

          <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>

and here is the 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>  

here is the php (which i am also new to)

        <?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";
        }

        ?>

I figured it out - this is due to a cross-domain access problem. I do not have the answer on how to solve it yet but wanted to post this so no one else spends time looking at my code, trying to fix it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM