首页 > 解决方案 > Login add type in php code but not working calling ajax not working

问题描述

I want to add type-wise define the role but not working fine and nor error show related PHP. I think ajax error but I have no idea gatting error. I am sharing login image and after login error show image so please help me...

Ajax Script here code

$('document').ready(function() { 
    /* handling form validation */
    $("#login-form").validate({
        rules: {
            password: {
                required: true,
            },
            user_email: {
                required: true,
                email: true
            },
        },
        messages: {
            password:{
              required: "please enter your password"
             },
            user_email: "please enter your email address",
        },
        submitHandler: submitForm   
    });    
    /* Handling login functionality */
    function submitForm() {     
        var data = $("#login-form").serialize();                
        $.ajax({                
            type : 'POST',
            url  : 'login.php',
            data : data,
            beforeSend: function(){ 
                $("#error").fadeOut();
                $("#login_button").html('<span class="glyphicon glyphicon-transfer"></span> &nbsp; sending ...');
            },
            success : function(response){                       
                if(response=="ok"){                                 
                    $("#login_button").html('<img src="ajax-loader.gif" /> &nbsp; Signing In ...');
                    setTimeout(' window.location.href = "welcome.php"; ',4000);
                } else {                                    
                    $("#error").fadeIn(1000, function(){                        
                        $("#error").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; '+response+' !</div>');
                        $("#login_button").html('<span class="glyphicon glyphicon-log-in"></span> &nbsp; Sign In');
                    });
                }
            }
        });
        return false;
    }   
});

PHP code here

<?php
session_start();
include_once("setting/config.php");

if(isset($_POST['login_button'])) {

    $user_email = trim($_POST['user_email']);
    $user_password = trim($_POST['password']);

    $sql = "SELECT uid, user, pass, type, email FROM users WHERE email='$user_email'";
    $resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
    $row = mysqli_fetch_assoc($resultset);  
    $type = $row['type'];
    if($row['pass']==$user_password){               
        $_SESSION['user_session'] = $row['uid'];

        //-----------type------------------
            $_SESSION['user_session'] = $row['uid'];
            if( $type==1){
            header("location: index.php");
            }elseif($type==2){
            header("location: admin.php");                                
            }elseif($type==3){
            header("location: user.php");
            }

        //-----------type------------------

    } else {                
        echo "email or password does not exist."; // wrong details 
    }       
}
?>

Login page

enter image description here

after login error show enter image description here

标签: javascriptphpajax

解决方案


推荐阅读