简体   繁体   English

为什么我的php代码无法重定向到任何php页面?

[英]Why won't my php code redirect to any php page?

Here is my index page where i can successfully login but wont redirect to any page? 这是我的索引页面,我可以在其中成功登录,但不会重定向到任何页面? just new been trying to get it to working. 刚刚尝试过使其工作。

<?php include('header.php'); ?>
<body>
<div class="container">
    <div style="height:50px;">
    </div>
    <div class="row" id="loginform">
        <div class="col-md-4 col-md-offset-4">
            <div class="login-panel panel panel-primary">
                <div class="panel-heading">
                    <h3 class="panel-title"><span class="glyphicon glyphicon-lock"></span> Login
                        <span class="pull-right"><span class="glyphicon glyphicon-pencil"></span> <a style="text-decoration:none; cursor:pointer; color:white;" id="signup">Sign up</a></span>
                    </h3>
                </div>
                <!-- Body For Login -->
                <div class="panel-body">
                    <form role="form" id="logform">
                        <fieldset>
                            <div class="form-group">
                                <input class="form-control" placeholder="Username" name="username" id="username" type="text" autofocus>
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="Password" name="password" id="password" type="password">
                            </div>
                            <button type="button" id="loginbutton" class="btn btn-lg btn-primary btn-block"><span class="glyphicon glyphicon-log-in"></span> <span id="logtext">Login</span></button>
                        </fieldset>
                    </form>
                     <!-- Body For Login -->
                </div>
            </div>
        </div>
    </div>

    <div class="row" id="signupform" style="display:none;">
        <div class="col-md-4 col-md-offset-4">
            <div class="login-panel panel panel-primary">
                <div class="panel-heading">
                    <h3 class="panel-title"><span class="glyphicon glyphicon-pencil"></span> Sign Up
                        <span class="pull-right"><span class="glyphicon glyphicon-log-in"></span> <a style="text-decoration:none; cursor:pointer; color:white;" id="login">Login</a></span>
                    </h3>
                </div>
                <div class="panel-body">
                    <!-- Body for Sign up -->
                    <form role="form" id="signform">
                        <fieldset>
                            <div class="form-group">
                                <input class="form-control" placeholder="Full Name" name="name" id="name" type="text" autofocus>
                            </div>

                            <div class="form-group">
                                <input class="form-control" placeholder="Email" name="email" id="email" type="text" autofocus>
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="Username" name="susername" id="susername" type="text" autofocus>
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="Password" name="spassword" id="spassword" type="password">
                            </div>
                            <button type="button" id="signupbutton" class="btn btn-lg btn-primary btn-block"><span class="glyphicon glyphicon-check"></span> <span id="signtext">Sign Up</span></button>
                        </fieldset>
                    </form>
                     <!-- Body for Sign up -->
                </div>
            </div>
        </div>
    </div>

    <div id="myalert" style="display:none;">
        <div class="col-md-4 col-md-offset-4">
            <div class="alert alert-info">
                <center><span id="alerttext"></span></center>
            </div>
        </div>
    </div>
</div>
<script src="custom.js"></script>
</body>
</html>

i don't know why but just keep going back to index.page 我不知道为什么,只是继续回到index.page

<?php 
    include('conn.php');
    session_start();
    if(isset($_POST['logform'])){
        $username=$_POST['username'];
        $password=md5($_POST['password']);

        $query=$conn->query("select * from users where username='$username' and password='$password'");

        if ($query->num_rows>0) {
            $row=$query->fetch_array();
            if ($row['level']=="admin") {
               $_SESSION['users']=$row['id']; 
               $_SESSION['level']=$row['level'];
               header('location: admin/index.php');
            }elseif ($row['level']=="agent") {
               $_SESSION['users']=$row['id']; 
               $_SESSION['level']=$row['level'];
               header('location: agent/index.php');
            }elseif ($row['level']=="user") {
               $_SESSION['users']=$row['id']; 
               $_SESSION['level']=$row['level'];
               header('location: agent/index.php');
            }else{
            ?>
                <span>Login Failed. User not Found.</span>
            <?php 
            }


            }


        }

Here is my login action form it working but will not redirect to any page that i set them i don't know why but im trying my best how to work around the problem 这是我的登录操作表格,它可以正常工作,但不会重定向到我设置它们的任何页面,我不知道为什么,但是我正在尽最大努力解决问题

Here is my custom.js which tell the php what to do next 这是我的custom.js,它告诉php接下来要做什么

$(document).ready(function(){
    //bind enter key to click button
    $(document).keypress(function(e){
        if (e.which == 13){
            if($('#loginform').is(":visible")){
                $("#loginbutton").click();
            }
            else if($('#signupform').is(":visible")){
                $("#signupbutton").click();
            }
        }
    });

    $('#signup').click(function(){
        $('#loginform').slideUp();
        $('#signupform').slideDown();
        $('#myalert').slideUp();
        $('#signform')[0].reset();
    });

    $('#login').click(function(){
        $('#loginform').slideDown();
        $('#signupform').slideUp();
        $('#myalert').slideUp();
        $('#logform')[0].reset();
    });

    $(document).on('click', '#signupbutton', function(){
        if($('#susername').val()!='' && $('#spassword').val()!=''){
            $('#signtext').text('Signing up...');
            $('#myalert').slideUp();
            var signform = $('#signform').serialize();
            $.ajax({
                method: 'POST',
                url: 'signup.php',
                data: signform,
                success:function(data){
                    setTimeout(function(){
                    $('#myalert').slideDown();
                    $('#alerttext').html(data);
                    $('#signtext').text('Sign up');
                    $('#signform')[0].reset();
                    }, 2000);
                } 
            });
        }
        else{
            alert('Please input both fields to Sign Up');
        }
    });

    $(document).on('click', '#loginbutton', function(){
        if($('#username').val()!='' && $('#password').val()!=''){
            $('#logtext').text('Logging in...');
            $('#myalert').slideUp();
            var logform = $('#logform').serialize();
            setTimeout(function(){
                $.ajax({
                    method: 'POST',
                    url: 'login.php',
                    data: logform,
                    success:function(data){
                        if(data==''){
                            $('#myalert').slideDown();
                            $('#alerttext').text('Login Successful. User Verified!');
                            $('#logtext').text('Login');
                            $('#logform')[0].reset();
                            setTimeout(function(){
                                location.reload();
                            }, 2000);
                        }
                        else{
                            $('#myalert').slideDown();
                            $('#alerttext').html(data);
                            $('#logtext').text('Login');
                            $('#logform')[0].reset();
                        }
                    } 
                });
            }, 2000);
        }
        else{
            alert('Please input both fields to Login');
        }
    });
});

Try to use 尝试使用

<input type ="submit" id="loginbutton">

instead of 代替

  <button type="button" id="loginbutton">

to post your form. 发布您的表格。

Change 更改

header('location: admin/index.php');

to

header('Location: agent/index.php');

it is an easy mistake to make 这是一个容易犯的错误

You forgot to define the method and action in form tag. 您忘记了在表单标签中定义methodaction The method attribute specifies how to send form-data. method属性指定如何发送表单数据。 When posting a form, it always good to have a method is post . 发布表单时,最好使用post方法。 As for action, a route or file location where action should perform. 至于动作,是应该执行动作的路径或文件位置。

So try this: 所以试试这个:

<form method="post" action="/{_action_filename_}.php">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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