doujun5009 2016-12-28 18:04
浏览 51
已采纳

Ajax用户注册在提交问题时失败

I am trying to implement an ajax modal signup form where users can register for the site. I have managed to get the login form working, but I am hitting a snag on registration. I took my login modal template as reference. Where did I go wrong?

UPDATE I've managed to get the database to register the user..and I've updated the code below. Few things now.

  1. The page doesnt redirect to homepage upon registration
  2. It doesnt have the user logged in when I do refresh the page
  3. Checking phpMyAdmin, the password the user inputs is not what is found in the database (a random string of text is there)
  4. When I use this random string of text OR the password that they registered with, the login fails.

My HTML.

<div id="signup-body" class="toggleMe">
                    <form class="form" id="signupform" role="form" name="signupform" action="" method="post">
                        <p class="status"></p> <!-- testing the status -->
                        <div class="form-group">
                            <label class="sr-only" for="signup-name">Username</label>
                            <input type="text" class="form-control" id="signupname" name="signup-name" placeholder="Username">
                        </div>
                        <!-- Form Group -->
                        <div class="form-group">
                            <label class="sr-only" for="signup-email">Email</label>
                            <input type="email" class="form-control" id="signupemail" name="signup-email" placeholder="Email">
                        </div>
                        <div class="form-group">
                            <label class="sr-only" for="signup-password">Password</label>
                            <input type="password" class="form-control" id="signuppassword" name="signup-password" placeholder="Password">
                        </div>
                        <!-- Form Group -->

                        <input type="submit" class="btn btn-secondary signup-modal-button" value="Sign Up">
                        <?php wp_nonce_field( 'ajax-login-nonce', 'security' ); ?>
                    </form>
                </div>

JQuery Code

jQuery(document).ready(function($) {

    // Perform AJAX login on form submit
    $('form#signupform').on('submit', function(e){
        $('form#signup p.status').show().text('Sending user info, please wait...');
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: "<?php echo admin_url( 'admin-ajax.php' );?>",
            data: {
                action: 'register_user', //calls wp_ajax_nopriv_ajaxlogin
                'username': $('form#signupform #signupname').val(),
                                'email': $('form#signupform #signupemail').val(),
                'password': $('form#signupform #signuppassword').val(),
                'security': $('form#signupform #security').val() },

            success: function(data){
                    console.log(data);
                $('form#signup p.status').text(data.message);
                if (data.loggedin == true){
                    document.location.href = ajax_login_object.redirecturl;
                }
            },error: function (data) {
                console.log(data);
            }

        });
        e.preventDefault();
    });

});

In the functions.php

function ajax_signup_init(){

wp_register_script('ajax-signup-script', get_template_directory_uri() . '/assets/js/ajax-signup-script.js', array('jquery') );
wp_enqueue_script('ajax-signup-script');

wp_localize_script( 'ajax-signup-script', 'ajax_signup_object', array(
    'ajaxurl' => admin_url( 'admin-ajax.php' ),
    'redirecturl' => home_url(),
    'loadingmessage' => __('Sending user info, please wait...')
));

// Enable the user with no privileges to run ajax_login() in AJAX

}
add_action('init', 'ajax_signup_init');

function ajax_signup(){

    // First check the nonce, if it fails the function will break
    check_ajax_referer( 'ajax-login-nonce','security');

    // Nonce is checked, get the POST data and sign user on
    $info = array();
    $info['user_login'] = $_POST['username'];
    $info['user_email'] = $_POST['email'];
    $info['user_password'] = $_POST['password'];
    $info['remember'] = true;


    $user_signup = wp_insert_user( $info);

    if (!is_wp_error($user_signup) ){
         echo "User created : ". $user_signup;
    }

    die();
}

add_action( 'wp_ajax_register_user', 'ajax_signup' );
add_action( 'wp_ajax_nopriv_register_user', 'ajax_signup' );
  • 写回答

2条回答 默认 最新

  • dongmeijian1716 2016-12-31 02:59
    关注

    Your ajax callback function should be like below.

            function ajax_signup(){
    
                // First check the nonce, if it fails the function will break
                check_ajax_referer( 'ajax-login-nonce','security');
    
                // Nonce is checked, get the POST data and sign user on
                $info = array();
                $info['user_login'] = $_POST['username'];
                $info['user_email'] = $_POST['email'];
                $info['user_password'] = $_POST['password'];
                $info['remember'] = true;
    
    
                $user_signup = wp_insert_user( $info);  
    
                if (!is_wp_error($user_signup) ){
                    echo json_encode(array('loggedin'=>true, 'message'=>__('Sign up successful, redirecting...')));
                } else {
                    echo json_encode(array('loggedin'=>false, 'message'=>__('Wrong username or password.')));
                }
                wp_set_current_user($user_signup);
                wp_set_auth_cookie($user_signup);
                die();
            }
    
            add_action( 'wp_ajax_ajax_login', 'ajax_login' );
            add_action( 'wp_ajax_nopriv_ajax_login', 'ajax_login' );
    

    You have to add these two in your code.

       wp_set_current_user($user_signup);
       wp_set_auth_cookie($user_signup);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助