我正在开发一个Ajax不起作用的插件。我正在使用以下代码:
add_action( \'wp_ajax_cpm_add_update\', array( $this, \'edit_added_people\' ) );
The
edit_added_people()
调用操作后未调用函数
cpm_add_update
, 这可能是什么原因?
以下是我的表单代码:
function cpm_add_people_form( $project_id, $people = null ) {
$title = $content = \'\';
$submit = __( \'Add Person\', \'cpm\' );
$users = array();
$id = $milestone = 0;
$action = \'cpm_add_user_new\';
if ( !is_null( $people ) ) {
$id = $people->ID;
$title = $people->post_title;
$name = $people->display_name;
$email = $people->user_email;
$files = array();
$users = empty( $project->users ) ? array() : $project->users;
$submit = __( \'Update New People\', \'cpm\' );
$action = \'cpm_add_people_update\';
}
ob_start();
?>
<div class="cpm-milestone-form-wrap" id="add_people-form">
<h3 style="background: none repeat scroll 0 0 #ccc; color: #222;font-size: 1.3em; margin: -0.5em -0.5em 2em;padding: 12px; text-align: center; width: 579px;"><?php _e(\'User Detail Form \', \'cpm\'); ?></h3>
<form class="cpm-add-people-form" action="" method="post" id="cpm-user-edit-here">
<table cellspacing="0" cellpadding="5">
<?php wp_nonce_field($action); ?>
<tr>
<td><b>User Name : </b></td><td><input name="user_name" style="width:100%;" type="text" id="people_name" value="<?php echo esc_attr($people->user_login); ?>" class="required cpm-jitender display_error_body"></td>
</tr>
<tr>
<td><b>Email address : </b></td><td><input name="people_email" style="width:100%;" type="text" id="people_email" value="<?php echo esc_attr($people->user_email); ?>" class="required cpm-jitender email"></td>
</tr>
<?php if (!is_null($people)) {
$first_name = get_user_meta($people->ID, \'first_name\', true);} ?>
<tr>
<td><b>First Name : </b></td><td><input name="people_first_name" style="width:100%;" type="text" id="people_first_name" value="<?php echo esc_attr($first_name); ?>"></td>
</tr>
<?php if (!is_null($people)) {
$last_name = get_user_meta($people->ID, \'last_name\', true);} ?>
<tr>
<td><b>Last Name : </b></td><td><input name="people_last_name" style="width:100%;" type="text" id="people_last_name" value="<?php echo esc_attr($last_name); ?>"></td>
</tr>
<tr>
<td><b>Display Name : </b></td><td><input name="people_display_name" style="width:100%;" type="text" id="people_display_name" value="<?php echo esc_attr($people->display_name); ?>"></td>
</tr>
<tr>
<td><b>Website : </b></td><td><input name="people_website" style="width:100%;" class="url" type="text" id="people_website" value="<?php echo esc_attr($people->user_url); ?>"></td>
</tr>
<tr>
<td><b>New Password : </b></td><td><input name="people_password" style="width:100%;" type="password" id="pass1" value=""></td>
</tr>
<!-- uncomment this to generate a password-->
<!-- <tr>
<td><b><?php // echo \'New password: \' . wp_generate_password( 8, false ); ?></b></td>
</tr>-->
<tr>
<td><b>Confirm Password : </b></td><td><input name="repeat_password" style="width:100%;" type="password" id="pass2" value="" data-rule-equalto="input[name=people_password]" data-msg-required="Please confirm your email address" data-msg-equalto="Email addresses do not match"></td>
</tr>
<div id="pass-strength-result"><?php _e(\'Strength indicator\'); ?></div>
<tr>
<td><b>Send Password : </b></td><td><input id="send_password" type="checkbox" value="1" name="send_password">Send this password to the new user by email.</td>
</tr>
<tr>
<td><b>Role : </b></td><td><select name="role" id="role" class="required cpm-jitender" style="width:50%;">
<?php
$current_role = $people->roles[0] ;
if (!$new_user_role)
$new_user_role = !empty($current_role) ? $current_role : get_option(\'default_role\');
wp_dropdown_roles($new_user_role);
?>
</select>
</td>
</tr>
</table>
<div class="cpm-attachment"> <?php cpm_upload_userimg_field($id, $files); ?></div>
<?php do_action(\'cpm_add_people_form\', $project_id, $people); ?>
<div class="submit">
<input type="hidden" name="action" value="<?php echo $action; ?>" />
<input type="hidden" name="project_id" value="<?php echo $project_id; ?>" />
<?php if ($id) { ?>
<input type="hidden" name="people_id" value="<?php echo $id; ?>" />
<?php } ?>
<input type="submit" name="create_new_people" id="create_new_people" class="button-primary" value="<?php echo esc_attr($submit); ?>">
<a id="CancleButton" class="button add-people-cancel" href="#"><?php _e(\'Cancel\', \'cpm\'); ?></a>
</div>
</form>
</div>
<?php
return ob_get_clean();
}
在此之后
ajax.php
:
add_action( \'wp_ajax_nopriv_cpm_add_update\', array( $this, \'edit_added_people\' ) );
function edit_added_people() {
check_ajax_referer(\'cpm_add_update\');
$posted = $_POST;
$people_id= $posted[\'people_id\'];
$people_obj = CPM_People::getInstance();
$added_user_id = $people_obj->Add_user_update($people_id);
if ($added_user_id) {
$response = array(
\'success\' => true,
\'id\' => $added_user_id,
\'content\' => cpm_task_html($task, $project_id, $list_id),
);
} else {
$response = array(\'success\' => false);
}
echo json_encode($response);
exit;
}