page redirection in jquery

余生长醉 提交于 2019-12-25 04:22:07

问题


I want to redirect the page after login succesfully completed. but the problem in this code is, if i give wrong input, it also go to home page. so i removed this statement 'window.location = home.php'. what could i do now? does anyone help me guys?

<script>
                $(document).ready(function(){
                    $('#reg_form').parsley();
                    $('#reg_form').on('submit',function(event){
                        event.preventDefault();

                        if($('#reg_form').parsley().isValid())
                        {
                            $.ajax({
                                url:"db.php",
                                method:"POST",
                                data:$(this).serialize(),

                                beforeSend:function(){
                                $('#submit').attr('disabled','disabled');
                                $('#submit').val('submitting the value');
                                },
                                    success:function(data){
                                        $('#reg_form')[0].reset();
                                        $('#reg_form').parsley().reset();
                                        $('#submit').attr('disabled',false);
                                        $('#submit').val('submit');
                                        $('#message').html(data);

                                    }

                            });
                        }
                    });
                });
            </script>

db.php

<?php

//user login
if(isset($_POST['email']))
{
        $email =  $_POST['email'];
        $password = $_POST['password'];

    $query = "SELECT * FROM tbl_register where email ='$email' AND password = '$password'";
    $result = mysqli_query($connect,$query);

    if(mysqli_num_rows($result)>0)
    {
        echo "login successfully completed";

    }
    else 
    {
        echo "login failed";
    }
}

?>

回答1:


Its better to pass flag value that login successfully or not from server side.

Like:

If you will write below code:

 if(mysqli_num_rows($result)>0)
    {
        echo 1;

    }
    else 
    {
        echo 0;
    }

And then in ajax call you can check data

 $.ajax({
                                url:"db.php",
                                method:"POST",
                                data:$(this).serialize(),

                                beforeSend:function(){
                                $('#submit').attr('disabled','disabled');
                                $('#submit').val('submitting the value');
                                },
                                    success:function(data){
                                         if(data==1){
                                        window.location = home.php
                                        }
                                       $('#reg_form')[0].reset();
                                        $('#reg_form').parsley().reset();
                                        $('#submit').attr('disabled',false);
                                        $('#submit').val('submit');
                                        $('#message').html(data);

}
});

I hope my answer helps you.




回答2:


Please always use the sessions in such login scenarios. you can set the session value if the login information is correct. Then check the session value in home.php, redirect the page from home.php to login page if session value is not correct. use the following code.

if(mysqli_num_rows($result)>0)
{
    echo 1;
    $_SESSION["is_logged_in"]="1";

}
else 
{
    echo 0;
    $_SESSION["is_logged_in"]="-1";
}

Now in "home.php" check whether the user is logged in or not.

if ($_SESSION["is_logged_in"]==-1){

     //redirect back to login page

}else if ($_SESSION["is_logged_in"]==1){
     //do nothing

}



回答3:


In your php file

if(mysqli_num_rows($result)>0)
{
    echo json_encode(array("status"=>1,"msg"=>"login successfully completed"));

}
else 
{
    echo json_encode(array("status"=>0,"msg"=>"login failed"));
}

and in ajax success check for the status from data as

 if(data.status){
      // success code and redirect code

   }else{
       // failed
   }


来源:https://stackoverflow.com/questions/54303475/page-redirection-in-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!