0

在这里尝试了一些解决方案,但它没有按预期工作。每次单击“登录”按钮时,登录页面都会不断刷新。我现在的代码:

索引.php

<?php include('header.php'); ?>
<body id="login">
    <div class="container">
        <div class="row-fluid">
            <div class="span6"><div class="title_index"><?php include('title_index.php'); ?></div></div>
            <div class="span6"><div class="pull-right"><?php include('login_form1.php'); ?></div></div>
        </div>
        <div class="row-fluid">
            <div class="span12"><div class="index-footer"><?php include('link.php'); ?></div></div>
        </div>
            <?php include('footer.php'); ?>
    </div>
<?php include('script.php'); ?>
</body>
</html>

login_form.php

<form id="login_form1" class="form-signin" method="post">
    <h3 class="form-signin-heading"><i class="icon-lock"></i> Sign in</h3>
    <input type="text" class="input-block-level" id="username" name="username" placeholder="Username" required>
    <input type="password" class="input-block-level" id="password" name="password" placeholder="Password" required>
    <button data-placement="right" title="Click Here to Sign In" id="signin" name="login" class="btn btn-info" type="submit"><i class="icon-signin icon-large"></i> Sign in</button>
    <script type="text/javascript">
        $(document).ready(function(){
            $('#signin').tooltip('show');
            $('#signin').tooltip('hide');
        });
    </script>
</form>
<script>
    jQuery(document).ready(function(){
        jQuery("#login_form1").submit(function(e){
            e.preventDefault();
            var formData = jQuery(this).serialize();
            $.ajax({
                type: "POST",
                url: "login.php",
                data: formData,
                success: function(html){
                    if(html=='true')
                    {
                        $.jGrowl("Loading File Please Wait......", { sticky: true });
                        $.jGrowl("Welcome to Learning Management System", { header: 'Access Granted' });
                        var delay = 1000;
                        setTimeout(function(){ window.location = 'dashboard_teacher.php'  }, delay);  
                    }else if (html == 'true_student'){
                        $.jGrowl("Welcome to Learning Management System", { header: 'Access Granted' });
                        var delay = 1000;
                        setTimeout(function(){ window.location = 'student_notification.php'  }, delay);  
                    }else
                    {
                        $.jGrowl("Please Check your username and Password", { header: 'Login Failed' });
                    }
                }
            });
            return false;
        });
    });
</script>

登录.php

<?php
include('dbcon.php');
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
/* student */
$conn = mysqli_connect('localhost','root','','capstone');
$result = mysqli_query($conn, "SELECT * FROM student WHERE username='$username' AND password='$password'")or die(mysqli_error($conn));
$num_row = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
/* teacher */
$conn = mysqli_connect('localhost','root','','capstone');
$query_teacher = mysqli_query($conn, "SELECT * FROM teacher WHERE username='$username' AND password='$password'")or die(mysql_error($conn));
$num_row_teacher = mysqli_num_rows($query_teacher);
$row_teahcer = mysqli_fetch_array($query_teacher);
if( $num_row > 0 ) 
{ 
    $_SESSION['id']=$row['student_id'];
    echo 'true_student';    
}
else if ($num_row_teacher > 0)
{
    $_SESSION['id']=$row_teahcer['teacher_id'];
    echo 'true';

}
else
{ 
    echo 'false';
}   

?>

顺便说一句,我有两个登录名,管理员和用户。管理员登录工作得很好,而用户登录(这个)总是在登录页面上刷新。我对管理员使用了相同的代码。谢谢!

4

2 回答 2

0

尝试将其更改<button><input>withtype="submit"

于 2018-01-22T12:01:04.060 回答
0

您需要更改 login_form.php 的代码:

<form id="login_form1" class="form-signin" method="post">
    <h3 class="form-signin-heading"><i class="icon-lock"></i> Sign in</h3>
    <input type="text" class="input-block-level" id="username" name="username" placeholder="Username" required>
    <input type="password" class="input-block-level" id="password" name="password" placeholder="Password" required>
    <button data-placement="right" title="Click Here to Sign In" id="signin" name="login" class="btn btn-info" type="button"><i class="icon-signin icon-large"></i> Sign in</button>
    <script type="text/javascript">
        $(document).ready(function(){
            $('#signin').tooltip('show');
            $('#signin').tooltip('hide');
        });
    </script> </form> <script>
    jQuery(document).ready(function(){
        jQuery("#login_form1").submit(function(e){
            e.preventDefault();
            var formData = jQuery(this).serialize();
            $.ajax({
                type: "POST",
                url: "login.php",
                data: formData,
                success: function(html){
                    if(html=='true')
                    {
                        $.jGrowl("Loading File Please Wait......", { sticky: true });
                        $.jGrowl("Welcome to Learning Management System", { header: 'Access Granted' });
                        var delay = 1000;
                        setTimeout(function(){ window.location = 'dashboard_teacher.php'  }, delay);  
                    }else if (html == 'true_student'){
                        $.jGrowl("Welcome to Learning Management System", { header: 'Access Granted' });
                        var delay = 1000;
                        setTimeout(function(){ window.location = 'student_notification.php'  }, delay);  
                    }else
                    {
                        $.jGrowl("Please Check your username and Password", { header: 'Login Failed' });
                    }
                }
            });
            return false;
        });
    }); </script>
于 2018-01-22T13:15:39.153 回答