2014-10-16 70 views
0

我試圖使用wp_mail和AjaxWordPress的阿賈克斯成功響應

電子郵件作品創造WordPress的接觸形式,但我在與阿賈克斯成功響應的問題。

電子郵件發送後,我需要向用戶顯示電子郵件已發送的消息。

的Javascript功能來驗證表單,並顯示成功消息:

$atj(function(){ 
     $atj('#training-submit').click(function(e) { 
     e.preventDefault(); 
     e.stopPropagation(); 
     if(verfiyFields()) { 
      alert('here'); 
      requestData = { 
       'action' : 'myajax-submit', 
       'firstName' : $atj("#name").val(), 
       'email' : $atj("#email").val(), 
      } 
      $atj.post(MyAjax.ajaxurl, requestData).done(function(result){   
      result = jQuery.parseJSON(result); 
      console.log(result); 
      if(result == 'success'){ 
       $atj('.training-form [type=text]').val(''); 
       $atj('.training-form-message').append('<p class="training-form-complete-message">Thank you for the email</p>'); 
      } 
      }); 
     } 
     }); 
    }) 



    //Verfiy 
    function verfiyTrainingFields() { 
     var flag = true; 

     var name =  $atj('#name'); 
     var email =  $atj('#email'); 

     if(name.val().indexOf(' ') === -1){ 
     name.parent().prepend('<p class="form-error">Please enter name, first space last</p>'); 
     errorMessage($atj('.form-error')); 
     flag = false; 
     } 
     if(!IsEmail(email.val())){ 
     email.parent().prepend('<p class="form-error">Please enter valid email address</p>'); 
     errorMessage($atj('.form-error')); 
     flag = false; 
     } 
     return flag; 
    } 

functions.php文件發送電子郵件,然後發送Ajax響應。

function myajax_submit() { 

     $name = sanitize_text_field($_POST['firstName']); 
     $email = sanitize_text_field($_POST['email']); 

     $headers[] = 'From: ' . $name . ' <' . $email . '>' . "\r\n"; 
     $headers[] = 'Content-type: text/html' . "\r\n"; //Enables HTML ContentType. Remove it for Plain Text Messages 

     $to = '[email protected]'; 

     $message = 'Name: ' . $name . "\r\n" . 'email: ' . $email; 

     add_filter('wp_mail_content_type', 'set_html_content_type'); 
     wp_mail($to, 'Email Test', $message); 
     remove_filter('wp_mail_content_type', 'set_html_content_type'); 

     echo 'email sent'; 

     // generate the response 
     $response = json_encode(array('success')); 

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

     exit; 
    } 

電子郵件發送和「電子郵件發送」回聲火災,但在JS的if(result == 'success'){不起作用。

js文件中的console.log(result)給出了以下內容。

<br /> 
<b>Warning</b>: call_user_func_array() expects parameter 1 to be a valid callback, function 'set_html_content_type' not found or invalid function name in <b>/Users/user.name/Desktop/MAMP-DocRoot/appname/src/php/wp-includes/plugin.php</b> on line <b>192</b><br /> 
email sent<br /> 
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /Users/user.name/Desktop/MAMP-DocRoot/appname/src/php/wp-includes/plugin.php:192) in <b>/Users/user.name/Desktop/MAMP-DocRoot/appname/src/php/wp-content/themes/sitename/functions.php</b> on line <b>66</b><br /> 
["success"] 
+0

移動這一點 - '標題( 「內容類型:應用程序/ JSON」);'到頂部你的PHP文件。在您開始輸出AJAX響應的正文('echo'email sent';')之後,您正在嘗試發送HTTP標頭時出現錯誤。 – 2014-10-16 20:05:10

回答

0

不能echo'in響應後設置headers。刪除 echo 'email sent';

0

首先,刪除echo語句,因爲它過早發送標題。

然後,你需要明確地告訴jQuery的崗位預計回JSON:

$atj.post(MyAjax.ajaxurl, requestData).done(function(result){ 
    result = jQuery.parseJSON(result); 
    console.log(result); 
    if(result == 'success'){ 
     $atj('.training-form [type=text]').val(''); 
     $atj('.training-form-message').append('<p class="training-form-complete-message">Thank you for the email</p>'); 
    } 
}, 'json'); 

最後,你的JSON編碼的迴應,不會給你預計response == success。相反,使用json_encode(array('success' => 1'));和改變你的JavaScript來:

if(1 == result.success)){ 
} 

這裏有一個完整的例子:

$.post(url, data, function(response){ 
    if(1 == response.success){ 
     // Success - do something 
    } else { 
     // Failure 
    } 
}, 'json'); 

<?php 
function myAjaxFunction(){ 
    $result = array(
     'success' => 0 // Assume failure first 
    ); 
    if($_POST[ 'whatever' ] == 'what i want'){ 
     $result[ 'success' ] = 1; 
    } 
    print json_encode($result); 
    exit(); 
} 
+0

喬 - 新九,我試過你的建議,但我仍然沒有得到在JS迴應。在控制檯中,我收到錯誤'Uncaught SyntaxError:Unexpected token <' – user51442 2014-10-17 07:06:21

+0

我剛剛嘗試了我給你的代碼,它完美地工作。將您的帖子更改爲以下內容並讓我知道在控制檯中輸入的內容:'$ atj.post(MyAjax.ajaxurl,requestData,function(result){console.log(result); }) – 2014-10-17 12:28:42

+0

Joe - New Nine ,對於延遲響應的道歉,我嘗試了你的建議代碼,並在控制檯中得到空值。 – user51442 2014-10-18 13:32:11