2015-07-21 78 views
0

這是我的錯誤功能代碼錯誤消息內嵌輸入

function output_errors($errors) { 
    return '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>'; 
} 

這是登錄我的PHP代碼

include 'core/init.php'; 

if (empty($_POST) === false) { 
    $username = $_POST['username']; 
    $password = $_POST['password']; 

    if (empty($username) === true || empty($password) === true) { 
     $errors[] = 'Enter a valid username or password.'; 
    } 
     elseif (user_exists($username) === false) { 
     $errors[] = 'User does not exist.'; 
     } 
     elseif (user_active($username) === false) { 
     $errors[] = 'Please activate your account first.'; 
     } 
     else { 

     if (strlen($password) > 32) { 
      $errors[] = 'Password too long. <br>'; 
     } 

     $login = login($username, $password); 

     if ($login === false) { 
      $errors[] = 'Incorrect username or password.'; 
     } 
      else { 
      $_SESSION['user_id'] = $login; //login returns user_id 
      header('Location: hashtagphotowall.php'); 
      exit(); 
      } 
     } 

    //visual representation of error arrays 
    //print_r($errors); 

} 
    else { 
    $errors[] = 'No data received!'; 
    } 

if (empty($errors) === false) { 
    echo output_errors($errors); 
} 

這是我的HTML登錄表單的代碼

<div id="firstModal" class="reveal-modal" data-reveal aria-hidden="true" role="dialog"> 
    <p class="modal-row"><em>Sign In.</em><hr></p> 
    <a href="#" class="button small">Connect with Facebook</a> 
    <p class="hr-enclosed">or login with email</p> 

    <form action="login.php" method="post"> 
     <div class="field-box"> 
     <input type="text" name="username" placeholder="Username:" /> 
     <input type="password" name="password" placeholder="Password:" /> 
     <input type="submit" name="loginbutton" class="button small" value="LOGIN" />      
     </div> 
    </form> 

    <p class="modal-row-sub">forgot your password? get help <a href="#">here</a>.<br>new user? sign up <a href="#" data-reveal-id="secondModal">here</a>.</p> 
    <a class="close-reveal-modal" aria-label="Close">&#215;</a> 
</div> 

的代碼起作用但錯誤消息上的另一個網頁上顯示。我試着聲明一個沒有值的變量,並用輸入標籤內聯回顯它,但我沒有輸出。也許是因爲我錯誤地使用了錯誤數組。任何幫助,我怎樣才能使錯誤數組嵌入在登錄表單輸入標籤謝謝

+0

支架是否正確平衡? if(empty($ _ POST)=== false){'...'else {error} [] ='沒有收到數據!'; }這是正確的邏輯嗎?不應該有選擇..如果沒有錯誤轉到新頁面,如果錯誤,重新顯示錶格並告訴用戶需要修復什麼... – zipzit

+0

是的,它是平衡的。代碼正在工作。我剛添加了'沒有收到數據'。這裏的問題是我不知道如何顯示與html輸入標籤內嵌的錯誤信息。 – monsteruniversity0908

+0

您的* html表單*和*登錄腳本*在同一頁上? –

回答

0

我想你想要的測試邏輯內置到相同的login.php文件中作爲窗體顯示。這樣一來,它便於將數據傳遞給用戶。

(僞代碼)

Add a div to the top of the form: `<div id="errors">output_errors($errors) </div>` 
which tells user what to do. Obviously first time thru $errors = "" (or one space?) 
The error div will be invisible to the user. 

If previous submission made, test the data for validity. 

If previous submission is made and input is good, go on to new page 
(Success_thank_you_for_your_submission.php) 

If previous submission is made, and input is not valid, 
redisplay the page, only now the errors will be on top of the form. 
redisplay with header("location:[login.php]"); 

You should take the time to re-populate all the fields the user originally submitted. 
Sending back an empty form when there are errors won't make folks happy. 

用戶保持提交表單,直至其右......

0

修改你的PHP登錄驗證和檢查代碼:

if (empty($errors) === false) { 
    // store error details into the session variable 
    $_SESSION['validation_errors'] = $errors; 
    // then redirect back to login page. 
    header('Location: login_page.php'); 
} 

上的登錄表單,在其中移動output_errors()函數。

function output_errors($errors) { 
    echo '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>'; 
} 

然後將此代碼添加到您的登錄表單上。這就是錯誤將(通常是表單之前)顯示:

<?php if(isset($_SESSION['validation_errors'])): ?> 
<div class="validation_error_box"> 
    <?php output_errors($_SESSION['validation_errors']); ?> 
</div> 
<?php endif; ?> 

獎金:如果登錄憑據無效,您可以添加用戶名到$ _SESSION數組然後顯示回登錄頁上。