PHP mail()函数没有通知jQuery成功/失败

PHP mail()循环正在运行,并正确发送,没有错误。 但是,我的jQuery无法正常工作,要在前端通知成功或错误消息。

PHP:

 add_action( 'wp_ajax_invfr_process_ajax', 'invfr_sendmail'); add_action( 'wp_ajax_nopriv_invfr_process_ajax', 'invfr_sendmail'); function invfr_sendmail() { $post = ( !empty( $_POST ) ) ? true : false; if( $post ) { $subject = invfr_get_settings( 'subject' ); $message = invfr_get_settings( 'message' ); $friends = $_POST['friend_email']; $errors = array(); foreach ( $friends as $key => $friend ) { $name = stripslashes( $_POST['friend_name'][$key] ); $email = trim( $_POST['friend_email'][$key] ); // Check name if( !$name ) $errors[] = '#friend_name-' . $key; if( !$email ) $errors[] = '#friend_email-' . $key; if( $email && !is_email( $email ) ) $errors[] = '#friend_email-' . $key; } // send email if( !$errors ) { foreach ( $friends as $key => $friend ) $mail = wp_mail( $email, invfr_tokens_replacement( $subject, $_POST, $key ), invfr_tokens_replacement( $message, $_POST, $key ) ); if( $mail ) echo 'sent'; } else echo json_encode( $errors ); } } 

jQuery的:

  var jQuery = jQuery.noConflict(); jQuery(window).load(function(){ jQuery('#invfr_form').submit(function() { // change visual indicators jQuery('td').removeClass('error'); jQuery('.loading').show(); jQuery('.submit input').attr('disabled', 'disabled'); // validate and process form here var str = jQuery(this).serialize(); jQuery.ajax({ type: 'POST', url: '', data: str, success: function(msg) { jQuery('#invfr_note').ajaxComplete(function(event, request, settings) { msg = msg.replace(/(\s+)?.$/, ""); if( msg == 'sent' ) { result = '

'; jQuery('#invfr_form input[type=text], #invfr_form input[type=email]').val(''); } else { //loop through the error items to indicate which fields have errors msg = msg.replace(/[\[\]']+/g,''); msg = msg.split(','); jQuery.each( msg, function ( i, id ) { id = id.replace(/["']{1}/g, ''); jQuery(id).parent('td').addClass('error'); }); result = '

<?php _e( 'ERROR: Check your form for the errors which are highlighted below.', 'invfr' ); ?>

'; //result = msg; msg = ''; } jQuery(this).html(result); // visual indicators jQuery('.loading').hide(); jQuery('.submit input').removeAttr('disabled'); }); } }); return false; }); });

谢谢你的帮助!

以下是一些通用步骤。 请注意,这些是示例,而不是完整的代码。 您必须找到在您的案例中应用此信息的正确方法。

首先,在WP中,将您的函数添加为ajax回调:

 add_action ( 'wp_ajax_my_action', 'invfr_sendmail' ); // The AJAX command is 'my_action' 

您必须对您的function进行一些修改。 一方面,回应事情不会给出答复。 但是我们从一开始就开始吧! 在回调函数invfr_sendmail ,添加从jQuery接收表单的代码。 由于它们是一个编码字符串,我们必须在顶部解析它们:

 $my_form_data = array(); // Create an empty array parse_str( $_POST['my_form_data'], $my_form_data ); // Fills $my_form_data 

现在,您使用$my_form_data['fieldname']而不是使用$_POST['fieldname'] $my_form_data['fieldname']

在PHP代码的最后,您必须将JSON编码的回复发送到jQuery。 例如:

 $success = true; $errors = array( 'This one was wrong', 'That one too' ); $some_other_value = false; // Put these variables in an array $results = compact( 'success', 'errors', 'some_other_value' ); wp_send_json( $results ); 

通过AJAX发送表单并听取答案。

 jQuery('form#my_form_name').on('submit', function(event){ event.preventDefault(); // Stops the form from being submitted via POST var data = { command: 'my_action', // The same name you used in WP 'add_action' above my_form_data: jQuery('form#my_form_name').serialize() }; jQuery('.loading').show(); jQuery.ajax({ type: 'POST', url: ajaxurl, data: data }).done( function( response ) { // This runs when the server replies something if (response.success) { // This corresponds to the $success variable we set jQuery('#someElement').html('The operation was a success!') } if (response.errors) { // This would be the $errors array from PHP response.errors.each( function(errorMessage) { jQuery('#someElement').append( errorMessage ); } ); } if (response.some_other_value) { // Here nothing happens if $some_other_value in PHP was false } }).fail( function( jqXHR, textStatus, errorThrown ) { // This runs when the request fails (ie timeout) jQuery('#someElement').html( 'The AJAX request failed with error message ' + errorThrown ); }).always( function() { // This runs always after an AJAX request, even a failed one jQuery('.loading').hide(); }); }); 

希望这个例子能让你在使用插件时顺利完成任务!