简体   繁体   English

WordPress登录页面上的Ajax表单

[英]Ajax form on the wordpress login page

I have an issue on my code. 我的代码有问题。 I have created a form to allow user to ask for a "new account" to our customer services. 我创建了一个表格,允许用户向我们的客户服务请求“新帐户”。 The form ask informations and on click on the submit, send an email with ajax. 表格询问信息,然后单击提交,发送带有ajax的电子邮件。 Simply as that, but it is not working. 就这么简单,但是它不起作用。 My php file called with ajax request is not called (code not executed) like the ajax function didn't take the "URL" parameter. 我的用ajax请求调用的php文件没有被调用(代码未执行),就像ajax函数没有采用“ URL”参数一样。 But ajax request return success. 但是ajax请求返回成功。

So the problem is my email is not send. 所以问题是我的电子邮件没有发送。

If you can help me, many thanks in advance. 如果您能帮助我,请先谢谢。

 function submit_new_account_form() { //AJAX envoi contact $("#form_new_account").submit(function(){ var name = $("#name").val(); var email = $("#email").val(); var ap21code = $("#ap21code").val(); var store = $("#store").val(); var Suburb = $("#Suburb").val(); var postcode = $("#postcode").val(); var contact_number = $("#contact_number").val(); var request; if( name !== "" && email !== "" && ap21code !== "" && store !== "" && Suburb !== "" && postcode !== "" && contact_number !== ""){ request = $.ajax({ url: "http://www.globebrandb2b.com/?page_id=39", method : "POST", data: { name : name, email : email, ap21code: ap21code, store: store, Suburb: Suburb, postcode: postcode, contact_number: contact_number } }); request.success(function() { $("#name").val(''); $("#email").val(''); $("#ap21code").val(''); $("#store").val(''); $("#Suburb").val(''); $("#postcode").val(''); $("#contact_number").val(''); $("#after_send").html("<p><div style=' opacity:1 !important; background-color: #ed1c24; color:white; max-height:70px; border: none;' id='success' class='alert alert-success' style='margin-bottom: 0px;'><p><i class='glyphicon glyphicon-alert' style='margin-right: 5px;'></i>Your request has been send.</p></div></p>"); $("#after_send").show(); $("#submit_new_account").attr('disabled','disabled'); setTimeout(function() { $("#form_new_account").hide(); $("#submit_new_account").removeAttr('disabled'); $("#after_send").hide(); }, 5000); }); request.fail(function() { $("#after_send").html('<p><div id="error" class="alert alert-info col-xs-12 col-sm-12 col-md-12 col-lg-12"><p><i class="glyphicon glyphicon-info-sign"></i>An error occured, please try later.</p></div></p>'); }); } return false; }); } 
 mail( 'Dylan.S@globebrand.com', 'TEST GLOBE EUROPE', 'TEST GLOBE MESSAGE'); 
 <form id="form_new_account" name="form_new_account" method="POST"> <table> <tr> <th class="padding"><label for="name" class="col-2 col-form-label">Name:</label></th> <td><input type="text" class="form-control" id="name" name="name" required></td> </tr> <tr> <th class="padding"><label for="email" class="col-2 col-form-label">Email Address:</label></th> <td><input type="text" class="form-control" id="email" name="email" required></td> </tr> <tr> <th class="padding"><label for="ap21code" class="col-2 col-form-label">AP21 Customer Account Code:<br><i>(on your statement)</i></label></th> <td><input type="text" class="form-control" id="ap21code" name="ap21code" required></td> </tr> <tr> <th class="padding"><label for="store" class="col-2 col-form-label">Store Name:</label></th> <td><input type="text" class="form-control" id="store" name="store" required></td> </tr> <tr> <th class="padding"><label for="Suburb" class="col-2 col-form-label">Suburb:</label></th> <td><input type="text" class="form-control" id="Suburb" name="Suburb" required></td> </tr> <tr> <th class="padding"><label for="postcode" class="col-2 col-form-label">PostCode:</label></th> <td><input type="text" class="form-control" id="postcode" name="postcode" required></td> </tr> <tr> <th class="padding"><label for="contact_number" class="col-2 col-form-label">Contact Number:</label></th> <td><input type="text" class="form-control" id="contact_number" name="contact_number" required></td> </tr> </table> <input id="submit_new_account" type="submit" name="submit_new_account" class="button" value="Submit" onclick="submit_new_account_form();"/> <p id="after_send"></p> </form> 

Back when starting WordPress development I read it was best to use WordPress's internal AJAX engine for requests. 回到开始WordPress开发时,我读到最好使用WordPress的内部AJAX引擎进行请求。 Since then that's how I've been doing it. 从那以后,这就是我一直在做的事情。 It's a little bit of extra work but it seems relevant in a situation like this. 这需要一些额外的工作,但在这种情况下似乎很重要。

I do believe the URL is your issue. 我相信URL是您的问题。 Someone can correct me if I'm wrong, but the URL http://www.globebrandb2b.com/?page_id=39 isn't going to post back to the same page (to run mail() with your form data) if that's what you're trying to accomplish. 如果我错了,有人可以纠正我,但是如果出现以下情况,URL http://www.globebrandb2b.com/?page_id=39不会发回到同一页(使用表单数据运行mail() )这就是您要完成的工作。

To utilize the WordPress's internal AJAX, take a look a below... 要利用WordPress的内部AJAX,请在下面查看...

In your functions.php , where you enqueue your scripts: 在您的functions.php中 ,将脚本排入队列:

wp_enqueue_script('site', get_stylesheet_directory_uri() . '/js/site.js');

// In my projects I pass a small array of variables I want to use in my JS
// One of them is the url for the admin-ajax.php    
$params = array(
    'ajaxURL' => admin_url('admin-ajax.php')
);

// Localize scripts
wp_localize_script('site', 'jsData', $params);

IMPORTANT! 重要! wp_localize_script() MUST be called after the script has been registered using wp_register_script() or wp_enqueue_script(). 必须使用wp_register_script()或wp_enqueue_script()注册脚本后才能调用wp_localize_script()。

What that will do is output a variable to your page like: 要做的是将变量输出到您的页面,例如:

<script type='text/javascript'>
var jsData={"ajaxURL":"http:\/\/www.example.com\/wp-admin\/admin-ajax.php"};
</script>

Then you'll want to update your AJAX request to use the URL you provdied, and on top of that you'll want to tell it what function to run, in this case you'd create a new_account_mail() function in your functions.php. 然后,您需要更新AJAX请求以使用您提供的URL,最重要的是,您要告诉它要运行的函数,在这种情况下,您将在函数中创建new_account_mail()函数。的PHP。

request = $.ajax({
     url: jsData.ajaxURL, // <----- admin-ajax.php
     method : "POST",
     data: { action: 'new_account_mail', // <---- name of the function in your functions.php
             form: { name : name, // <---- form data
                     email : email, 
                     ap21code: ap21code, 
                     store: store, 
                     Suburb: Suburb, 
                     postcode: postcode, 
                     contact_number: contact_number 
                     }
      }
   });

Alright, so now in your functions.php , you'll create a function to handle that AJAX submission: 好了,所以现在在您的functions.php中 ,您将创建一个函数来处理该AJAX提交:

// Add action for `wp_ajax_nopriv_your function name` 
// and `wp_ajax_your function name` so that it works for authenticated 
// and unauthenticated users.
add_action( 'wp_ajax_nopriv_new_account_email', 'new_account_email' );
add_action( 'wp_ajax_new_account_email', 'new_account_email' );
function new_account_email(){

    // Now this function receives your $_POST data

   // Get the form data
   $form = [];
   parse_str($_POST['form'], $form); // parses query string into variables
    $name = $form['name'];
    $email = $form['email'];
    $ap21code = $form['ap21code'];
    // and so on....

    $data = [];

    // Send the mail
    // I like to use a 3rd party library like SwiftMailer
    // then do an if/else to determine if there were issues sending
    if(mail($email, 'TEST GLOBE EUROPE', 'TEST GLOBE MESSAGE')){
          $data['sent'] = 'The message was sent!';
    }else{
          $data['fail'] = 'The message was NOT sent!';
    }

    // In my project, I return a json object
    $response = json_encode($data);

    // Response output
    header( "Content-Type: application/json" );
    echo $response;

    // Stop execution
    wp_die();
}

Finally! 最后! Back to your AJAX: 返回您的AJAX:

// The request could come back without failing, but that doesn't 
// indicate the message was successfully sent
request.success(function(response){
    if(response.sent){
            // Message was sent
            // Maybe... :)
            // Even if the email was accepted for delivery, 
            // it doesn't mean it's actually sent and received
    }else{
            // Message was not sent
            // response.fail will have the fail message in it
    }
})
request.fail(function(error){
    // The request itself failed
});

There you have it! 你有它! It's a bit of extra work but I've used this method many times. 这是额外的工作,但我已经使用了很多次此方法。

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

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