2015-07-21 54 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数组然后显示回登录页上。