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

标签 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(); 
?>

最佳答案

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

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

然后回显“用户名或密码错误”;可以通过单页表单处理轻松完成。

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>

另一种方法是通过 Ajax 请求 checkuser-pass.php

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

关于javascript - 如何在登录表单上方显示错误的用户名和密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39436382/

相关文章:

javascript - 无法动态加载MathJax

PHP Soap 服务器响应格式

php - .htaccess 默认目录行为

html - 类别/子类别列表的 CSS 样式

html - div布局定位有问题

javascript - If Condition 比较两个字符串格式 HH :MM:SS

javascript - JQuery 延迟。使用 $.when 和 .progress()

javascript - Angular 5 在路由器中使用 Angular cli 非延迟加载模块

php - 将 Facebook 的好友列表数组与 MySQL 表进行比较

java - 尝试使用 JavaMail 通过 Outlook 在 HTML 电子邮件中发送 CSS