输入错误消息内联

时间:2015-07-21 05:43:37

标签: php arrays forms error-handling

  

这是我的错误函数代码

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>
  

代码正在运行,但错误消息显示在另一个网页上。我尝试声明一个没有值的变量,并使用输入标签内联回显,但我没有输出。也许是因为我错误地使用了错误数组。有关如何使用登录表单中的输入标记内联错误数组的任何帮助? 谢谢

2 个答案:

答案 0 :(得分: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.

用户不断提交表格直到...

答案 1 :(得分: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; ?>

BONUS:如果登录凭据无效,您可以将用户名添加到$ _SESSION数组中,然后将其显示在登录页面上。