2012-03-08 192 views
0

我想從表單發送一些表單數據到PHP進行驗證,並在數據有效時發送電子郵件。我的形式是下面相應的jQuery的Ajax代碼:通過AJAX的POST請求無法在PHP中識別

<h1><a href="#" onclick="show_form();">Send Invitation</a></h1> 
<form id="invitation-form" action="" method="post"> 
<p> 
    Your Email Address<br /> 
    <input type="text" name="your_email" /> 
</p> 
<p> 
    Email Address of Recipient<br /> 
    <input type="text" name="friends_email" /> 
</p> 
<p> 
    Subject<br /> 
    <input type="text" name="subject" /> 
</p> 
<p> 
    Message<br/> 
    <textarea rows="10" cols="20" name="message">' . $post_url . '</textarea> 
</p> 
<input type="hidden" name="form_type" value="sendinvitation" /> 
<p><input type="submit" name="submit" value="Send" /></p> 
</form> 
<div id="note"></div> 
<script type="text/javascript"> 
jQuery("#invitation-form").submit(function() { 
    alert("submit has fired"); 
    var str = jQuery(this).serialize(); 
    jQuery.ajax({ 
     type: "POST", 
     url: "http://127.0.1.1/wordpress/wp-content/plugins/send-invitation/send_email.php", 
     data: str, 
     success: function(message) { 
      jQuery("#note").ajaxComplete(function(event, request, settings) { 
       var result = ""; 
       if (message == "OK") { 
        result = "Your message was sent successfully"; 
       } 
       else if (message == "NOT OK") { 
        result = "Email could not be sent"; 
       } 
       else { 
        result = "Email could not be sent because of unknown reason"; 
       } 
       jQuery(this).html(result); 
      }); 
     } 
    }); 
    alert("After post"); 
    return false; // Disables submit button from sending a real request 
}); 
</script> 

我的請求被成功發送到send_email.php但我似乎並沒有得到任何POST數據或就此獲取數據。我對send_email.php文件中的代碼是在這裏:

<?php 
require_once('send-invitation.php'); 

if (isset($_POST)) { 
    $post_url = get_post_url(); 
    echo("Before send invitation"); 
    sendinvitation_form_action($post_url); 
    echo("After send invitation"); 

    echo("Back in send email"); 
} 

function get_post_url() { 
    global $post; 
    $blog_id = get_option('page_for_posts'); 
    $post_id = ''; 

    if (is_home($blog_id)) { 
     $post_id = $blog_id; 
    } else { 
     $post_id = $post->ID; 
    } 

    return get_permalink($post_id); 
} 
?> 

處理表單數據是一個類裏面,看起來像這樣的代碼:

public function sendinvitation_form_action($post_url) { 
    $action = ''; 
    if ($_SERVER['REQUEST_METHOD'] != 'POST') 
    { 
     error_log('Show Form', 0); 
     $action = $this->sendinvitation_get_form($post_url); 
    } 
    else if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
     error_log('Handle Form', 0); 
     $action = $this->sendinvitation_handle_submit($post_url); 
    } 
    return $action; 
} 

public function sendinvitation_handle_submit() { 
    error_log('Inside handle_submit', 0); 
    $success = __('OK'); 
    $error = __('NOT OK'); 
    if (isset($_POST['form_type']) and ($_POST['form_type'] == 'sendinvitation')) { 
     try { 
      error_log('Inside if', 0); 
      $to = $_POST['friends_email']; 
      $subject = $_POST['subject']; 
      $message = $_POST['message']; 

      // TODO Suppress error message with @ 
      error_log("Before sending email"); 
      error_log("$to $subject $message", 0); 
      $send_result = wp_mail($to, $subject, $message); 
      error_log($send_result, 0); 
      $feedback = $send_result ? $success : $error; 
     } 
     catch(Exception $e) { 

      $feedback = "Catch: " . $e->getMessage(); 
     } 
    } else { 
     $feedback = "Error: " . $error; 
    } 
    error_log($feedback, 0); 
    echo $feedback; 
} 

以上會發生什麼事是$this->sendinvitation_get_form($post_url);被稱爲替代的$this->sendinvitation_handle_submit($post_url);

我在我的智慧結束爲什麼會發生這種情況。這與我正在撰寫的WordPress插件有關,但認爲這與使用JS和PHP而不是WordPress相關。

在此先感謝。 nav

+1

嘗試使用螢火蟲或提琴手確認發佈的數據正在發送到php頁面。 – AndrewR 2012-03-08 19:41:37

+0

一些(可能是不正確的)想法:我想'if(isset($ _ POST)){'需要是別的東西,因爲$ _POST可能總是被設置。我最終使用'!empty()'代替。另外,我不確定您的瀏覽器是否會確定127.0.0.1和本地主機的來源不同,但請注意。 – Guttsy 2012-03-08 19:44:00

+0

我建議閱讀本文[在WordPress中使用AJAX的正確方式](http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/)。你使用的方法屬於他的「在WordPress中糟糕的使用ajax」。 – 2012-03-08 22:47:48

回答

0

我認爲您在以下行上出現function not defined錯誤時失敗。

$blog_id = get_option('page_for_posts'); 

由於您直接調用send_email.php文件,並且它不包含任何Wordpress包含,因此未定義該函數。

+0

感謝您的回覆。我已經修改了上面的代碼,正如在5篇技巧文章中所說的那樣,在另一篇文章的幫助下(http://byronyasgur.wordpress.com/2011/06/27/frontend-forward-facing-ajax-in-wordpress /)在[插件中的AJAX](http://codex.wordpress.org/AJAX_in_Plugins)的wordpress codex頁面上引用,它的工作原理完美無瑕。 – navanitachora 2012-03-10 03:41:04