简体   繁体   中英

How to get return value from php to ajax

-- Ajax Code

   var User = function(){
    return {
        init : function(){
            document.getElementById('login').addEventListener('click', this.login);
        },
        login : function(){
            var username = $("#username").val(),
                password = $("#password").val();

            $.ajax({
                url : 'http://localhost/oc2/user_info/login',
                method : 'post',
                dataType : 'json',
                data : {
                    username : username,
                    password : password
                },
                success : function(response){   
                    alert('h'); <-- add the php return value id here.
                    window.location.href = "main.html";
                },
                error : function(response){
                    alert(response.responseText);                   
                }
            });
        }
    };
}();

-- PHP CodeIgniter

public function login()
    {
        $post = $this->input->post();

        $where = array(
            'email_address' => $post['username'], //"turbobpo.johnrey@gmail.com", 
            'password' => md5($post['password']) //"e10adc3949ba59abbe56e057f20f883e" 
        );

        $user_info = $this->get_by($where);



        if(isset($user_info['id']))
        {

            $this->session->set_userdata('user_info', $user_info);

            $response = array(
                'id' => $user_info['id'], <-- this i want to pass to my ajax
                'success' => TRUE
            );
        }
        else
        {
            $response = array(
                'success' => FALSE
            );
        }

        print json_encode($response);
    }

Hello can you help me for this part i already management to went far on this php ajax i'm not used in creating this application please i need help i place a comment on the codes to see where i wanna retrieve the value from php to my ajax code so i can use it on my next retrieving of file where i use the id of a login user to get his available access in a grid form. it would be a plus if you can also show me how can i use that data to pass it back in php again the id after retrieving it on the success ajax return array value.

var User = function(){
    return {
        init : function(){
            document.getElementById('login').addEventListener('click', this.login);
        },
        login : function(){
            var username = $("#username").val(),
                password = $("#password").val();

            $.ajax({
                url : 'http://localhost/oc2/user_info/login',
                method : 'post',
                dataType : 'json',
                data : {
                    username : username,
                    password : password
                },
                success : function(response){   
                    response.id; // Here is the id JQuery parses JSON for you
                    window.location.href = "main.html";
                },
                error : function(response){
                    alert(response.responseText);                   
                }
            });
        }
    };
}();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM