2016-07-29 291 views
0

首先我要感謝Codecourse的教程。 一切正常,但我想顯示錯誤到我的表td。 也許一個愚蠢的問題,但我是一個新手,並試圖用沒有運氣PHP OOP登錄/註冊系統

<?php 
require_once 'core/init.php'; 

if(Input::exists()) { 
    if(Token::check(Input::get('token'))) { 

     $validate = new Validate(); 
     $validation = $validate->check($_POST, array(
      'username' => array('required' => true), 
      'password' => array('required' => true) 
     )); 

     if($validate->passed()) { 
      $user = new User(); 

      $remember = (Input::get('remember') === 'on') ? true : false; 
      $login = $user->login(Input::get('username'), Input::get('password'), $remember); 

      if($login) { 
       Redirect::to('index.php'); 
      } else 
       echo "Incorrect username or password"; 

     } else { 
      foreach($validate->errors() as $error) { 
       echo $error, '<br>'; 
      } 
     } 
    } 
} 
?> 

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Document</title> 
</head> 
    <body> 
     <form action='' method='post'> 
      <table> 
       <tr> 
        <td><!-- display errors here --></td> 
        <td><!-- and/or here --></td> 
       </tr> 
       <tr> 
        <td><label for='username'>Username</label></td> 
        <td><input type='text' name='username' id='username'></td> 
       </tr> 
       <tr> 
        <td><label for='password'>Password</label></td> 
        <td><input type='password' name='password' id='password'></td> 
       </tr> 
       <tr> 
        <td></td> 
        <td><label for='remember'><input type='checkbox' name='remember' id='remember'>Remember me</label></td> 
       </tr> 
       <tr> 
        <td><input type='hidden' name='token' value='<?php echo Token::generate(); ?>'></td> 
        <td><input type='submit' value='Login'></td> 
       </tr> 
      </table> 
     </form> 
    </body> 
</html> 
+0

只需將foreach循環遍歷錯誤向下移動到您要顯示它們的位置即可。 – JimL

+0

將所有錯誤分配給一個變量($ error_messages)並在任何你想要的地方回顯。 –

+0

@JimL - 我不認爲這很簡單,我們必須將該循環中的錯誤添加到數組中,然後再執行一個額外的foreach循環爲什麼要使用@ArtisiticPhoenix – ArtisticPhoenix

回答

1

像這樣:

<?php 
require_once 'core/init.php'; 

$errors = array(); //storage variable 

if(Input::exists()) { 
    if(Token::check(Input::get('token'))) { 

     $validate = new Validate(); 
     $validation = $validate->check($_POST, array(
      'username' => array('required' => true), 
      'password' => array('required' => true) 
     )); 

     if($validate->passed()) { 
      $user = new User(); 

      $remember = (Input::get('remember') === 'on') ? true : false; 
      $login = $user->login(Input::get('username'), Input::get('password'), $remember); 

      if($login) { 
       Redirect::to('index.php'); 
      } else 
       $errors[] = "Incorrect username or password"; //add other errors in too! 

     } else { 
      foreach($validate->errors() as $error) { 
       $errors[] = $error; //add error to storage 
      } 
     } 
    } 
} 
?> 

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Document</title> 
</head> 
    <body> 
     <form action='' method='post'> 
      <table> 
       <tr> 
        <td><!-- display errors here --></td> 
        <td> 
         <?php foreach($errors as $error) : ?> 
          <?php echo $error; // output ?> 
         <?php endforeach; ?> 
        </td> 
       </tr> 
       <tr> 
        <td><label for='username'>Username</label></td> 
        <td><input type='text' name='username' id='username'></td> 
       </tr> 
       <tr> 
        <td><label for='password'>Password</label></td> 
        <td><input type='password' name='password' id='password'></td> 
       </tr> 
       <tr> 
        <td></td> 
        <td><label for='remember'><input type='checkbox' name='remember' id='remember'>Remember me</label></td> 
       </tr> 
       <tr> 
        <td><input type='hidden' name='token' value='<?php echo Token::generate(); ?>'></td> 
        <td><input type='submit' value='Login'></td> 
       </tr> 
      </table> 
     </form> 
    </body> 
</html> 

只需將它們存儲在一個變量中,然後將它們輸出,其中你想要的,漂亮的向前推進。