简体   繁体   English

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

[英]Submit not working after clicking an button element in html

I am new in web development so please understand me. 我是Web开发的新手,所以请理解我。

After I click the button to submit in PHP in HTML it only reloads my login.html 单击按钮以HTML格式提交PHP后,它只会重新加载我的login.html

Can someone help me to redirect the login page into index page if the credentials of the user is correct. 如果用户的凭据正确,有人可以帮助我将登录页面重定向到索引页面。

Here is the code: 这是代码:

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: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 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);
?>

That is because the action attribute in your form is empty 这是因为表单中的action属性为空

action=""

you need to point it your php file like this: 您需要将其指向您的php文件,如下所示:

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

If the action attribute is empty,it will point to/refresh the file that contains the form. 如果action属性为空,它将指向/刷新包含表单的文件。

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

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