2016-04-28 103 views
0

我最近完成了我的登錄系統,但我有一個關於PHP的問題。我怎樣才能將文本或HTML回顯到html的某個部分?PHP回聲到HTML的某個部分

例如。可以說下面的代碼都在一個PHP文件中。我怎麼能在php代碼中迴應2個迴應的評論是在哪裏?

因此,如果我要顯示此頁面,並且他們的登錄失敗,它會回顯「您輸入的用戶名和密碼與我們的記錄不符,請仔細檢查並重試。在HTML中的評論。

我希望有道理,謝謝!

<?php 
    session_start(); 

    if (isset($_POST['username']) && isset($_POST['password'])) { 
     include_once("db.php"); 
     $username = mysqli_real_escape_string($sqlcon, $_POST['username']); 
     $username = strtoupper($username); 
     $password = mysqli_real_escape_string($sqlcon, $_POST['password']); 

     for ($i = 0; $i < 1000; $i++) { 
      $password = hash(sha512, $password . $username); 
     } 

     $userQuery = "SELECT * FROM users WHERE username = '" . $username . "' LIMIT 1"; 
     $user = mysqli_query($sqlcon, $userQuery); 

     if (mysqli_num_rows($user) > 0) { 
      $user = mysqli_fetch_assoc($user); 
      $id = $user['id']; 
      $databasepass = $user['password']; 

      if ($password === $databasepass) { 
       $_SESSION['username'] = $username; 
       $_SESSION['id'] = $id; 
       header("Location: admin.php"); 
      } else { 
       echo "The username and password you entered did not match our records. Please double-check and try again."; 
      } 
     } else { 
      echo "The username and password you entered did not match our records. Please double-check and try again."; 
     } 
    } 
?> 

<!DOCTYPE html> 
<html> 
    <head> 
     <title>ADMIN</title> 
    </head> 
    <body> 
     <div> 
      <!--ECHO THOSE TWO ECHOS ABOVE HERE--> 
     </div> 
    </body> 
</html> 
+0

我個人建議尋找MVC。它有助於保持代碼清潔並將你的html與php分開 –

回答

3

內您可以將文本變量和使用PHP下你的HTML中回顯。

<?php 
    session_start(); 

    if (isset($_POST['username']) && isset($_POST['password'])) { 
     include_once("db.php"); 
     $username = mysqli_real_escape_string($sqlcon, $_POST['username']); 
     $username = strtoupper($username); 
     $password = mysqli_real_escape_string($sqlcon, $_POST['password']); 

     for ($i = 0; $i < 1000; $i++) { 
      $password = hash(sha512, $password . $username); 
     } 

     $userQuery = "SELECT * FROM users WHERE username = '" . $username . "' LIMIT 1"; 
     $user = mysqli_query($sqlcon, $userQuery); 

     if (mysqli_num_rows($user) > 0) { 
      $user = mysqli_fetch_assoc($user); 
      $id = $user['id']; 
      $databasepass = $user['password']; 

      if ($password === $databasepass) { 
       $_SESSION['username'] = $username; 
       $_SESSION['id'] = $id; 
       header("Location: admin.php"); 
      } else { 
       $result = "The username and password you entered did not match our records. Please double-check and try again."; 
      } 
     } else { 
      $result = "The username and password you entered did not match our records. Please double-check and try again."; 
     } 
    } 
?> 

<!DOCTYPE html> 
<html> 
    <head> 
     <title>ADMIN</title> 
    </head> 
    <body> 
     <div> 
      <?php echo $result; ?> 
     </div> 
    </body> 
</html> 
3

將代碼中的echo更改爲變量,例如。 $message = "......";

那麼你的DIV

<?php 

echo $message; 

?> 
0

如果$消息沒有設置它會拋出一個錯誤,那麼爲什麼不要麼使用:

<?=isset($message)?$message:"";?> 

可以在代碼初始化

$message = ""; 

然後顯示$消息/ $結果由Jacob規定見& rishal