简体   繁体   English

如何在Wordpress中使用我的AJAX电子邮件

[英]How do I get my AJAX email in Wordpress to work

Good Afternoon, 下午好,

I am having an extremely frustrating issue. 我有一个非常令人沮丧的问题。 I am currently trying to use an AJAX function to send an email from within a contact form in my Wordpress site. 我目前正在尝试使用AJAX功能从我的Wordpress网站的联系表单中发送电子邮件。 It only sends the email when none of the fields have been filled, but from the moment I try to submit data within the form fields, the email does not send. 它只会在没有填写任何字段时发送电子邮件,但从我尝试在表单字段中提交数据的那一刻起,电子邮件就不会发送。

Additionally, for some reason it seems to work when I am running it from outside the Wordpress environment, but from the moment I included it in the wordpress template file, then the problem starts to occur. 另外,由于某些原因,当我从Wordpress环境外部运行它时,它似乎工作,但从我将它包含在wordpress模板文件中的那一刻起,问题就开始发生了。

Here is my code below. 这是我的代码如下。 Forgive the fact that at the moment there isn't any error or spam handling: 原谅目前没有任何错误或垃圾邮件处理的事实:

This is the Javascript: 这是Javascript:

        $(document).ready(function(){
        $('#emailform').submit(function(){

            window.open('','',"width=500,height=150").document.write("Thank you for contacting us, we will reply you shortly. Thank you!");

            // getting all the stuff from the form
            var form = $(this),
            formData = form.serialize(),
            formUrl = form.attr('action'),
            formMethod = form.attr('method'),
            emailresponse = $('#emailresponse');

            // loading stuff
            emailresponse.fadeOut(200, function(){
                $(this).text("Message sent")
                .fadeIn(200)
            });


            // sending stuff to the server
            $.ajax({
                url: formUrl,
                type: formMethod,
                data: formData,
                success:function(data) {
                    //stuff to do when the ajax call is successful and complete

                    var responseData = $.parseJSON(data),
                        klass = '';

                    emailresponse.fadeOut(200, function(){
                        $(this).text(responseData.message)
                        .fadeIn(200);
                    });

                }
            })


            return false;
        })
    })

and this is the php 这是PHP

    if (isset($_GET['action'])) {

    if($_GET['action'] == 'email') {
        $name = $_POST['name'];
        $email = mysql_real_escape_string($_POST['email']);
        $message = $_POST['message'];

        $m1 = "Name of customer: " . $name . "\n";
        $m2 = "Customer's Email: " . $email . "\n";
        $m3 = "Message: " . $message;
        $emailmessage = $m1 . $m2 . $m3;

        mail('seedorf@me.com', 'Message from LUNASKYMODA.CO.UK contact page', $emailmessage);

        $data = array(  
                'status' => $status,  
                'message' => $message  
            );

        echo json_encode($data);

    exit;
    }
}

The web page in question is http://lunaskymoda.co.uk/contact-us/ 有问题的网页是http://lunaskymoda.co.uk/contact-us/

Ps, If it says "Message Sent" does not necessarily means that it sends.. Still working on that too. Ps,如果它说“发送消息”并不一定意味着它发送..仍然在努力。

Ok, you have a lot of different stuff going on. 好的,你有很多不同的东西在继续。 First, this is jQuery code (a javascript library) and your code is missing semi-colons all over the place. 首先,这是jQuery代码(一个javascript库),你的代码在整个地方缺少分号。 Try this: 尝试这个:

$(document).ready(function(){
        $('#emailform').submit(function(){

            window.open('','',"width=500,height=150").document.write("Thank you for contacting us, we will reply you shortly. Thank you!");

            // getting all the stuff from the form
            var form = $(this);
            formData = form.serialize();
            formUrl = form.attr('action');
            formMethod = form.attr('method');
            emailresponse = $('#emailresponse');

            // loading stuff
            emailresponse.fadeOut(200, function(){
                $(this).text("Message sent")
                .fadeIn(200);
            });


            // sending stuff to the server
            $.ajax({
                url: formUrl,
                type: formMethod,
                data: formData,
                success:function(data) {
                    //stuff to do when the ajax call is successful and complete

                    var responseData = $.parseJSON(data);
                    // klass = ''; <--what is this? you don't define 'klass' anywhere

                    emailresponse.fadeOut(200, function(){
                        $(this).text(responseData.message)
                        .fadeIn(200);
                    });

                }
            });


            return false;
        });
    });

Second, as soon as I pull up your site I get this error in console: 其次,只要我拉起你的网站,我就会在控制台中收到此错误:

Uncaught TypeError: Object [object Object] has no method 'livequery' wp-e-commerce.js:273

So your site has javascript errors before you even submit the script. 因此,在您提交脚本之前,您的网站会出现javascript错误。 Try and clear all errors as javascript has a way of breaking and not working at all if there are errors higher up in the chain. 尝试清除所有错误,因为如果链中存在更高的错误,javascript有一种破解方式并且根本不起作用。

Third, when I submit the form I get this error: 第三,当我提交表单时,我收到此错误:

POST http://lunaskymoda.co.uk/contact-us/?action=email 404 (Not Found)

This could be either a) because your JS is broken from the second error or b) because you still have html form actions when you are using jQuery to process the form. 这可能是a)因为你的JS从第二个错误或b)中断了,因为当你使用jQuery来处理表单时你仍然有html表单动作。 So: 所以:

Fourth, change your form tag from this: 第四,从这个改变你的表格标签:

<form id="emailform" action="?action=email" method="post" accept-charset="utf-8" novalidate="novalidate">

to the following: 以下内容:

<form id="emailform">

All this should get you on the right track. 所有这一切都应该让你走上正轨。 Troubleshooting JS becomes a lot easier when you look in the console for errors. 当您在控制台中查找错误时,JS的故障排除变得更加容易。 In chrome just right click anywhere and click "inspect element", then click "console". 在chrome中右键单击任意位置并单击“inspect element”,然后单击“console”。 In FireFox download the 'firebug' add on. 在FireFox中下载'firebug'添加。 Troubleshooting JS without console is like working in the dark. 没有控制台的JS故障排除就像在黑暗中工作。

Seems your contact form's action parameter is not placed properly. 似乎您的联系表单的操作参数未正确放置。

Check this for ref: 检查这个参考:

http://www.wp-themix.org/wordpress/how-to-add-a-jquery-ajax-contact-form-to-wordpress/ http://www.wp-themix.org/wordpress/how-to-add-a-jquery-ajax-contact-form-to-wordpress/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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