0

I am developing a plugin in which Ajax is not working. I'm using this code:

add_action( 'wp_ajax_cpm_add_update', array( $this, 'edit_added_people' ) ); 

The edit_added_people() function is not called after calling the action cpm_add_update, what could be the reason for this?

Here is my code for the form:

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();
}

After this on 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;
}
  • Have you added the code in your plugin's main file? – Domain Aug 10 '15 at 12:53
  • Can you edit your post to add your code? Be sure to indent and use the code formatting tools so that it's readable – Tom J Nowell Aug 10 '15 at 13:28
1

if the request is from an unauthenticated user, you should use wp_ajax_nopriv_(action) to call the function, which in your case is:

add_action('wp_ajax_nopriv_cpm_add_update', array($this,'edit_added_people'));
| improve this answer | |
  • still not solved my problem. it is reloading the page in spite of calling edit_added_people() – Rahul Upadhyay Aug 10 '15 at 13:30
  • can you post more code? – Brick Yang Aug 10 '15 at 13:32
  • @Rahul, if it's reloading the page, this means you might have issue in your JS code, either there is problem in JS code or you haven't added code to prevent from page load – Domain Aug 10 '15 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.