检查数据库后,如何在同一表单上显示登录错误消息


How do i display login error message on the same form after checking the database

index.php 这是我的索引文件,工作正常。 它的作用是检查用户会话变量并显示当前登录的用户。

<?php
    error_reporting(E_ALL ^ E_NOTICE);
    session_start();
    $username = $_SESSION['username']; 
?>
   <div id="userinfo">
   <?php
      if ( $username ) {
          echo "welcome <b>$username</b> <a href='logout.php'>Logout</a>";
      } else {
          echo 'welcome guest please <a href="login.php">Login</a>';
      }
   ?> 
   </div>

登录表单.php

<?php
    error_reporting(E_ALL ^ E_NOTICE);
    session_start();
    $username = $_SESSION['username'];
?>
<form id='frmlogin' method="post" action="esk.php">
    <div id="dialogcontainer">
        <div id="dialog">
           <h3>Welcome to Talk Login</h3>
           <p>Username</p>
           <input type="text" id="txtloginusername" name="txtloginusername"/>
           <p>Password</p>
           <input type="password" id="txtloginpassword" name ="txtloginpassword"/> <br /><br />
           <button type="submit">Login</button>
           <br>
           <br>
           <div id="logindisplay" class ="display"></div>
        </div>
    </div>        
 </form>

登录.php而不是将消息回显到其他页面,我希望它显示在登录表单上.php这恰好是我的登录表单。

 <?php
    error_reporting(E_ALL ^ E_NOTICE);
    session_start();
    $username = $_SESSION['username'];
    $user = $_POST['txtloginusername'];
    $pass = $_POST['txtloginpassword'];
    if ( $user ) {
        require ('connect.php');
        $query = mysql_query("SELECT * FROM user WHERE username = '$user'");
        $numrows = mysql_num_rows( $query );
        if ( $numrows == 1 ) {
           $row = mysql_fetch_assoc( $query );
           $dbpass = $row['password'];
           $dbuser = $row['username'];
           $dbactive = $row['active'];
           if ( $pass == $dbpass ) {
              if ( $dbactive == 1 ) {
                  $_SESSION['username'] = $dbuser;
                  header("location:index.php"); //"success";
              }
           } else {
                  echo "You did not enter the correct password"; //Display this somewhere on loginform.php
           }    
        } else {
           echo"Invalid Username"; // //Display this also in loginform.php 
        }
        mysql_close();
    } else {
        echo "Wrong username"; //this too;  
    } 
    ?>
Follow the Below Steps
1. use Ajax method to send the request from loginform.php page.
function validUser()
{   
      $.ajax({ 
      type: "POST",
      url: 'esk.php',
      data: $('#frmlogin').serialize(),
       success: function(data){
        $("#logindisplay").append(data);
       }
   });
  }
Step 2. remove submit button and change it to type button.
<input  type="button"  value="submit" onclick=validUser() />

step 3. remove the action of the form.
step 4. on esk.php page simply echo the message u want to show.
Thats It!!! 
您可以将

错误消息传递回登录表单,而不是在登录.php中回显结果。

例如,您可以通过以下方式将"错误的用户名"传递回登录表单:

header('Location: loginform.php?err=Wrong+username');
exit;

loginform.php中,将这些行添加到要显示错误的位置:

<?php
if(isset($_GET['err'])) { echo '<p>'. $_GET['err'] . '</p>'; }
?>