2016-12-04 102 views
-3

我正在創建一個使用PHP的網頁,從代碼沒有PHP錯誤,但當我點擊按鈕,它沒有做任何事情的網頁。我不知道發生了什麼,如果任何人都可以提供幫助,那將是非常感謝的。登錄頁面與PDO問題

<?php 
session_start(); 
if(isset($_SESSION['user_id'])){ 
    header("Location: /"); 
} 
require 'database.php'; 
if (!empty($_POST['username']) && !empty($_POST['password'])): 
    $records=$conn->prepare('select id, username, password from admin where username = :username'); 
    $records->bindParam(':username', $_POST['username']); 
    $records->execute(); 
    $results=$records->fetch(PDO::FETCH_ASSOC); 
    $message=''; 

    if(count($results)>0 && password_verify($_POST['password'], $results['password'])){ 
     $_SESSION['user_id']=$results['id']; 
     header("Location: /"); 
    }else{ 
     echo 'Sorry, wrong password'; 
} 
endif; 
?> 
<html> 
<head> 
    <link href='http://fonts.googleapis.com/css?family=Comfortaa' rel='stylesheet' type='text/css'> 
    <title>Admin Login</title> 
</head> 
<body> 
    <?php if(!empty($message)): ?> 
    <p><?=$message ?></p> 
    <?php endif; ?> 
    <h1>Admin Login</h1> 
    <form id='login' method='POST' action='login.php'> 
    <label> 
     Username: 
     <input type='text' name='username' placeholder='Enter your usename' id='username' size='20' maxlength='100'> 
    </label> 
    <br> 
    <label> 
     Password: 
     <input type='password' name='password' placeholder='Enter your password' id='password' size='20' maxlength='100' > 
    </label> 
    <br> 

    <button type='button'>Login</button> 
    </form> 
    <a href="register.php">Register</a> 
    <br/> 
    <style type='text/css'> 
     form{ 
      font-family: Verdana; 
      width: auto; 
      text-align: center; 
     } 
     label { 
      width: 150px; 
      font-size: 170%; 
      length: 200px; 
     }   
     body{ 
      background-color: lightblue; 
      text-align: center; 
      position: fixed; 
       top: 50%; 
       left: 50%; 
       margin-top: -100px; 
       margin-left: -200px; 
      height:200px; 
      width:400px; 
     } 

     button{ 
      padding:10px; 
      color:#fff; 
      background:#0098cb; 
      width:200px; 
      margin:20px auto; 
      margin-top:0px; 
      border:0px; 
      border-radius: 3px; 
      cursor:pointer; 

     }   
     input[type='text']{ 
      height:30px; 
      outline:none; 
      padding:10px; 
      width:200px; 
      border-radius: 3px; 
      border:1px solid #eee; 
      margin:20px auto; 
     } 
     button:hover{ 
      background:#00b8eb; 
     } 

</body> 
</html> 
+3

你想要輸入的密碼與用戶名相同,按鈕類型應該是提交類型 –

+0

哦,我已經固定了這部分。沒有任何改變。 –

回答

0

您的按鈕類型是按鈕,您需要更改要提交的類型。

您現在的按鈕:

<button type='button'>Login</button> 

什麼你的按鈕應該是

<button type='submit' name="btnName">Login</button> 

然後在你的PHP腳本檢查,如果該鍵設置,然後做你的等待處理

​​

按鈕類型='按鈕'v 。S型= '提交'

提交

按鈕形式數據提交到服務器。如果未指定屬性,或者屬性動態更改爲空值或無效值,則這是默認值。

按鈕:

按鈕沒有缺省行爲。它可以將客戶端腳本與元素的事件相關聯,這些事件在事件發生時觸發。

您窗體中發生的事情是因爲您有type =按鈕,所以您沒有與其事件關聯的客戶端腳本。

+0

非常感謝!它現在有效! –