2013-11-06 3 views
0

Цикл 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:

<script type="text/javascript"> 
      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: '<?php echo admin_url('admin-ajax.php'); ?>', 
          data: str, 
          success: function(msg) { 
           jQuery('#invfr_note').ajaxComplete(function(event, request, settings) { 
            msg = msg.replace(/(\s+)?.$/, ""); 
            if(msg == 'sent') { 
             result = '<div class="updated"><p><?php _e('Your invitation has been sent! Send another?', 'invfr'); ?></p></div>'; 
             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 = '<div class="error"><p><?php _e('<strong>ERROR:</strong> Check your form for the errors which are highlighted below.', 'invfr'); ?></p></div>'; 
             //result = msg; 
             msg = ''; 
            } 
            jQuery(this).html(result); 
            // visual indicators 
            jQuery('.loading').hide(); 
            jQuery('.submit input').removeAttr('disabled');      
           });     
          }      
         });     
        return false; 
       });   
      }); 
     </script> 

Спасибо за любую помощь!

+0

Вы уверены, что это не будет ошибка обратного вызова, которую вы не слушаете? Почему вы используете ajaxComplete INSIDE для успеха ajax? –

+0

Потому что я не хороший программист? Это мой лучший ответ. Я трясу эту вещь как 3 часа и не могу получить чертовски загрузочный гиф, чтобы перестать вращаться и дать мне успешное предупреждение. –

+0

Добавьте обратный вызов ошибки, аналогичный вашему обратному вызову. 'error: function() {console.log (arguments)}' –

ответ

0

Ниже приведены некоторые общие шаги. Обратите внимание, что это примеры, а не полный код. Вы должны найти правильные способы применения этой информации в своем случае.

Во-первых, в WP, добавить функцию в качестве обратного вызова Ajax:

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

Вы должны внести некоторые изменения в вашей функции. Во-первых, повторять то, что не даст ответа. Но начнем с самого начала! Внутри функции обратного вызова invfr_sendmail, добавьте код, который получает форму от jQuery. Так как они приходят в качестве закодированной строки, мы должны разобрать их на вершине:

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

Теперь, вместо того, чтобы использовать $_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 (i.e. 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(); 
    }); 
}); 

Надеюсь, что этот пример поможет вам с вашим плагином!

Смежные вопросы