我编写了一个插件向朋友发送邀请,并使用AJAX提交了一个表单,并使用了中讨论的技巧5 tips for using AJAX in wordpress 还有另一篇文章在插件插件wordpress codex页面中引用。
我现在已经按照预期执行了相关代码并发送了一封电子邮件,但是页面在AJAX调用完成之前会重新加载。
相关PHP代码如下:
<?php
define( \'SEND_INVITATION_MIN_WORDPRESS_VERSION\', \'3.1.1\' );
define( \'SEND_INVITATION_PLUGIN_URL\', plugins_url( \'\', __FILE__ ) );
// Tie into WordPress Hooks and any functions that should run on load.
//
add_action( \'init\', array( \'NC_SendInvitation\', \'send_invitation_init\' ) );
class NC_SendInvitation {
protected $nonce_name = \'send_invitation\';
protected $post_url = \'\';
public static function send_invitation_init() {
new self;
}
public function __construct() {
$this->send_invitation_head();
$this->send_invitation_check_wordpress_version();
add_action( \'show_invitation_form\', array( $this, \'send_invitation_get_form\' ), 10, 1 );
// Add AJAX actions
// Serves logged in users
add_action( \'wp_ajax_form_action\', array( $this, \'send_invitation_handle_submit\' ) );
// Serves non-logged in users
add_action( \'wp_ajax_nopriv_form_action\', array( $this, \'send_invitation_handle_submit\' ) );
}
public function send_invitation_head() {
// Add script that for handling AJAX POST request to the head
wp_enqueue_script( \'send-invitation-ajax-handle\',
plugins_url( \'js/ajax.js\', __FILE__ ),
array( \'jquery\' ) );
// Localise script find out what this does
wp_localize_script( \'send-invitation-ajax-handle\',
\'send_invitation_ajax_script\',
array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );
// Add the script for showing and hiding the form to the head
wp_enqueue_script( \'send-invitation-js\',
plugins_url( \'js/send-invitation.js\', __FILE__ ),
array( \'jquery\' ) );
// Add the CSS that styles the form to the head
wp_enqueue_style( \'send-invitation\',
plugins_url( \'css/send-invitation.css\', __FILE__ ) );
}
public function send_invitation_check_wordpress_version() {
global $wp_version;
$exit_msg = \'Send Invitation requires version \'
. SEND_INVITATION_MIN_WORDPRESS_VERSION
. \'or newer <a href="http://codex.wordpress.org/Upgrading_WordPress">Please
update!</a>\';
if ( version_compare( $wp_version, SEND_INVITATION_MIN_WORDPRESS_VERSION, \'<\') )
{
exit( $exit_msg );
}
}
public function send_invitation_get_form($post_url) {
// TODO Try moving this into another file and include here using method above
$send_invitation_form =\'
<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="nonce" value="send_invitation" />
<input type="hidden" name="action" value="form_action" />
<p><input id="submit-button" type="submit" value="Send" onclick="send_invitation();" /></p>
</form>
<div class="response-area">
<span class="error"></span>
<span class="success"></span>
</div>\';
echo $send_invitation_form;
}
public function send_invitation_handle_submit() {
$success = __(\'OK\');
$error = __(\'NOT OK\');
if (!empty($_POST)) {
try {
error_log(\'Inside if\', 0);
$to = $_POST[\'friends_email\'];
$subject = $_POST[\'subject\'];
$message = $_POST[\'message\'];
// TODO Suppress error message with @
$send_result = wp_mail($to, $subject, $message);
$feedback = $send_result ? $success : $error;
}
catch( Exception $e ) {
$feedback = "Catch: " . $e->getMessage();
}
} else {
$feedback = "Error: " . $error;
}
echo $feedback;
exit;
// Do not forget to exit
}
?>
相关javascript代码如下:
function send_invitation() {
alert("got here");
jQuery.post(send_invitation_ajax_script.ajaxurl,
jQuery(\'#invitation-form\').serialize(),
function(response) {
alert(\'Response: \' + response);
jQuery(\'.response-area .success\').html(response);
}
);
}
我看到的另一个问题是,对ajaxurl的POST请求在firebug中显示为红色,但它很快就会消失,我看不到发生了什么,但正确的PHP函数正在执行,但没有响应返回,我得到的只是在发送请求后重新加载页面。
我不知道为什么会出现这种情况,但我已经试着调试了一段时间,但没有找到解决方案。
提前谢谢。导航