通过AJAX的POST请求在PHP中无法识别

时间:2012-03-08 19:32:39

标签: php wordpress jquery

我正在尝试将一些表单数据从表单发送到PHP进行验证,如果数据有效则发送电子邮件。我的表单下面是相应的jQuery ajax代码:

<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数据或GET数据。我的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 个答案:

答案 0 :(得分:0)

我认为您在以下行中遇到function not defined错误。

$blog_id = get_option('page_for_posts');

由于您直接调用send_email.php文件,并且它不包含任何Wordpress包含,因此未定义该功能。