单击html中的按钮元素后提交不起作用

时间:2019-07-23 07:59:22

标签: php html

我是Web开发的新手,所以请理解我。

单击按钮以HTML格式提交PHP后,它只会重新加载我的login.html

如果用户的凭据正确,有人可以帮助我将登录页面重定向到索引页面。

代码如下:

HTML:

 <?php include('php/pLogin.php'); ?>
 <form action="" method="POST">
                <div class="login-form-head">
                    <h4>Sign In</h4>
                    <p>Hello there, Sign in and start managing your Admin 
                    Template</p>
                </div>
                <div class="login-form-body">
                    <div class="form-gp">
                        <label for="txtUser">Email address</label>
                        <input type="text" id="txtUser" name="txtUser">
                        <i class="ti-email"></i>
                    </div>
                    <div class="form-gp">
                        <label for="txtPass">Password</label>
                        <input type="password" id="txtPass" name="txtPass">
                        <i class="ti-lock"></i>
                    </div>
                    <div class="row mb-4 rmber-area">
                        <div class="col-6">
                            <div class="custom-control custom-checkbox mr-sm-2">
                                <input type="checkbox" class="custom-control-input" id="customControlAutosizing">
                                <label class="custom-control-label" for="customControlAutosizing">Remember Me</label>
                            </div>
                        </div>
                        <div class="col-6 text-right">
                            <a href="#">Forgot Password?</a>
                        </div>
                    </div>
                    <div class="submit-btn-area">
                        <button id="btnLogin" type="submit" value="Submit">Login <i class="ti-arrow-right"></i></button>
                    </div>
                    <div class="form-footer text-center mt-5">
                        <p class="text-muted">Don't have an account? <a href="register.html">Sign up</a></p>
                    </div>
                </div>
            </form>

PHP:Login.php

<?php
    include("pConfig.php");
    session_start();
    if($_POST['txtUser'] != '' && $_POST['txtPass'] !='') {
        $error = '';
        // username and password sent from form 
        echo $myusername = mysqli_real_escape_string($db,$_POST['txtUser']);
        echo $mypassword = mysqli_real_escape_string($db,$_POST['txtPass']); 
        //$sql = "SELECT user_id  FROM user WHERE username = '$myusername' and password = '$mypassword'";
        $sql = "SELECT * FROM userinfo WHERE (UserName = '$myusername' OR Email = '$myusername' ) and Password = '$mypassword'";
        $result = mysqli_query($db,$sql);
        $rows = mysqli_fetch_array($result);
        $count = mysqli_num_rows($result);
        // If result matched $myusername and $mypassword, table row must be 1 row
        if($count == 1) {
            //prevent session fixation attack
            session_regenerate_id();
            $_SESSION['login_user'] = $myusername; 
            header("Location: index.html");
        } else {
            $error = 'Incorrect UserName Or Password, Please Check your Credentials';
        }
    }
?>

Config.php

<?php
   define('DB_SERVER', 'localhost');
   define('DB_USERNAME', 'root');
   define('DB_PASSWORD', '');
   define('DB_DATABASE', 'ci');
   $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

1 个答案:

答案 0 :(得分:2)

这是因为表单中的action属性为空

action=""

您需要像这样将其指向您的php文件:

action="/path/to/your/file.php"

如果action属性为空,它将指向/刷新包含表单的文件。