如何在登录表单上方显示错误的用户名和密码?

时间:2016-09-11 13:02:41

标签: javascript php html css

有人可以帮我在登录表单上方显示错误的用户名和密码吗?它不应该重定向到其他页面,而是必须显示在登录表单的正上方 我的代码包含

<!Doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mainpagestyle.css">
</head>
<style>
body {
background:#99ffff;
height:100%;
width:100%;
}

#footer {
poition:absolute;
    padding: 1em;
    color: white;
    background-color: black;
    clear: left;
    text-align: center;
    width: 100%;
   border: 1px solid gray;
    bottom : 0;
   margin-top:90%;

}

</style>
<body>


<div class="container"> 
<h1>"MARK"</h1> 
<ul id="headline" class="fallingtextrotator" style="height:2em"> 
<div class="grad"></div> 
<form action="checkuser-pass.php" method="POST"> 
<div class="header"> 
<div>LOG<span>IN</span> </div> 
</div> 
</br> 
<div class="login"> 
User Name: <input type="text" name="username"><br> 
Password: &nbsp&nbsp <input type="password" name="password"><br><br> 
<input type="submit" name="submit" value="Login!"> 
&nbsp &nbsp &nbsp &nbsp <a href=" ">FORGOT PASSWORD</a></div> 
</div> 
</br> 
</form> 

<ul id="headline" class="fallingtextrotator" style="height:2em"> 
<div class="grad"></div> 
<form action="checkguest.php" method="POST"> 
<div class="headerr"> 
<div>SIGN<span>UP</span> </div> 
</div> 
</BR>

<div class="signup"> 
User Name: <input type="text" name="username"><br><br> 
Email Id:&nbsp&nbsp&nbsp <input type="email" name="emailid"><br> 
Password: &nbsp&nbsp <input type="password" name="password"><br><br> 
<input type="submit" name="submit" value="signup!"> 
</form> 

</div> 
</section> 
<section class="footer"> 
<h5> &#xA9; Navya Chowdary</h5> 
<p><a href="#">Carrers </a>&nbsp &nbsp&nbsp&nbsp<a href="#"> Blog</a> &nbsp 
&nbsp&nbsp&nbsp<a href="#">Bussiness</a> 
&nbsp &nbsp&nbsp&nbsp<a href="#"> About Us</a>&nbsp &nbsp&nbsp&nbsp<a 
href="#">Suggestions or Complaints</a> &nbsp &nbsp&nbsp&nbsp<a href="#">Bug</a> 
</section> 
</body> 

</html>

这是我的checkuser-pass.php

<?php 
session_start(); 
$host="127.0.0.1"; 
$username="root"; 
$password=""; 
$db="markconnect"; 
$tb="userform"; 
$mysqli = new mysqli($host,$username,$password,$db) or die(mysqli_error());; 
//DEFINE USERNAME AND PASSWORD 
$username=$_POST['username']; 
$password=$_POST['password']; 
//echo md5($password); exit;
// To protect MySQL injection 
$username = stripslashes($username); 
$password = stripslashes($password); 
$username = mysqli_real_escape_string($mysqli,$username); 
$password = mysqli_real_escape_string($mysqli,$password); 
$password = md5($password);
//echo $password; exit;

$sql="SELECT * FROM $tb WHERE username='$username' and password='$password'"; 
//echo $sql; exit;
$result=mysqli_query($mysqli,$sql); 
//43 Mysql_num_row is counting table row 
$count=mysqli_num_rows($result); 
//print_r($count); 

if($count==1){
$_SESSION['username'] = $username;
//include 'saver.php'; 
header("Location: saver.php");
} 

else { 
echo "Wrong Username or Password"; 
//  <p>Please enter both username and password.</p>


} 
ob_end_flush(); 
?>

1 个答案:

答案 0 :(得分:0)

如果您希望带有登录表单的页面处理请求,您必须将checkuser-pass.php中的PHP代码放在表单页面中,并且:

<form action="" method="POST"> 

然后回复&#34;错误的用户名或密码&#34 ;;可以通过单页表单处理轻松完成。

onepageformproccesing.php:

<?php
    if(isset($_POST['submit'])){
        echo $_POST['username'];
        //process the form
        //make the database query and check the password
        //if user does not confirm make the error variable 
        $error = "Username or Password not correct!";
    }
?>

<!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>Form Proccessing</title>
        </head>
        <body>
            <?php if(isset($error)) { ?>
            <div class="error">
                <?php echo $error; ?>
            </div>
            <?php } ?>  
            <form  method="post" action="">
                Username: <input type="text" name="username"><br>
                Password: <input type="password" name="password"><br>
                <input type="submit" name="submit" value="Go!">
            </form>
        </body>
    </html>

其他方法是向checkuser-pass.php发送Ajax请求

其他方法是使用两个页面表单处理,但是从checkuser-pass.php重定向回表单页面并使用会话变量来处理错误。

相关问题