2017-08-07 54 views
-1

我想要一個消息框,彈出每當我輸入錯誤的用戶名或密碼。我怎樣才能做到這一點?你可以使用jQuery或JavaScript或任何你感到舒服的東西。我的代碼已經在工作,所以無需調試它,我只是想知道的消息框如何我輸入時彈出錯誤細節如果用戶名或密碼錯誤,如何創建消息框?

代碼的形式......

<?php 
session_start(); 
?> 
<link rel="stylesheet" type="text/css" href="stylelog.css"> 
<html> 
<head> 
<title></title> 
<img src='imageslogin/background.png' style='position:fixed; 
top:0px;left:0px;width:100%;height:100%;z-index:-1;'> 

</head> 
<body> 
<header> 
<nav> 
    <div class="main-wrapper"> 
    <ul> 
     <li><a href="index.php"><img src='imageslogin/logo.png'></a></li> 
    </ul> 
     <div class="nav-login"> 
     <?php 
     if (isset($_SESSION['user_id'])){ 
      echo '<form action="includes/logout.inc.php" method="POST"> 
     <button type="submit" name="submit">Logout</button> 
     </form>'; 
     }else{ 
      echo '<form action= "includes/login.inc.php" method="POST"> 
    <input type="text" name="uid" placeholder="Username"required> 
    <input type="password" name="pwd" placeholder="Password" required> 
       <button type="submit" name="submit">Login</button> 
      </form>'; 
     } 
     ?> 

     </div> 
    </div> 
</nav> 
</header> 

代碼日誌中.....

<?php 
require_once 'dbh.inc.php'; 

if (isset($_POST['submit'])) 
{ 
$uid = $_POST['uid']; 
$pwd = $_POST['pwd']; 

if($uid == "" || $pwd == "") 
{ 
    echo '<div id ="errormsg">Please fill in all fields</div>'; 
} 
else 
{ 
    $stmt = $DBcon->prepare("SELECT * FROM tbl_users Where username=? 
AND password=?");//prepare database, connect query it. 

這是dbh.inc.php創建的POD

$stmt->bindParam(1, $_POST['uid']);//bind parameters 
    $stmt->bindParam(2, $_POST['pwd']);//bind parameters 
    $stmt->execute(); 
    $row = $stmt->fetch();//get the row of the user ID in the database 
    $user = $row['username']; 
    $pass = $row['password']; 
    $id = $row['user_id']; 
    $type = $row['type']; 

      if ($uid==$user && $pwd==$pass && $type == "Administrator") 
      { 
       session_start(); 
       $_SESSION['user_id'] = $id; 
       $_SESSION['username'] = $user; 
       $_SESSION['password'] = $pass; 
       $_SESSION['type'] = $type; 
       header("location: ../Main_page.php"); 
      }//check if user password and member type 
    matches the inputted user credentials and is an administrator, 
    it will open main.php 
      else if ($uid == $user && $pwd == $pass && $type == "Member") 
      { 
       session_start(); 
       $_SESSION['user_id'] = $id; 
       $_SESSION['username'] = $user; 
       $_SESSION['password'] = $pass; 
       $_SESSION['type'] = $type; 
       header("location: ../user.php"); 
      }//check if user password and member type matches the inputted  
     user credentials and is an member, it will open user.php 
      else 
      { 
    header("location: ../index.php?=Error_loging_in_user_not_found"); 
      //echo '<div id="errormsg">User Not Found, try again</div>';  
      } 
     } 
    } 
    ?> 
+0

您應該**嘗試自己編寫代碼**。在[**做更多的研究**之後](https://meta.stackoverflow.com/q/261592/1011527)如果你有問題**發佈你已經嘗試過**的**清楚的解釋不工作**並提供[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。閱讀[如何問](http://stackoverflow.com/help/how-to-ask)一個很好的問題。請務必[參觀](http://stackoverflow.com/tour)並閱讀[this](https://meta.stackoverflow.com/q/347937/1011527)。 –

+0

**切勿存儲純文本密碼!**請使用***的[內置函數](http://jayblanchard.net/proper_password_hashing_with_PHP.html)***來處理密碼安全性。如果您使用的PHP版本低於5.5,則可以使用'password_hash()'[兼容包](https://github.com/ircmaxell/password_compat)。 ***在散列之前,不需要[escape passwords](http://stackoverflow.com/q/36628418/1011527)***或使用其他任何清理機制。這樣做會改變密碼並導致不必要的附加編碼。 –

回答

0

ÿ你可以通過echo作爲一個元素輸出你需要的信息,然後使用document.getElementById('your_id')得到它並粘貼到你的元素,然後你必須使用css進行格式化。

相關問題