首先,我要感谢Codecourse的教程。 一切都有效,但我想将错误显示在我的表td中。 也许是一个愚蠢的问题,但我是一个初学者,并试图用自己的方式解决这个问题
<?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 :(得分: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>
只需将它们存储在变量中,然后将它们输出到您想要的位置,非常直接。
答案 1 :(得分:0)
将所有错误推送到数组,然后在表中显示所有这些错误。
Array_push: http://php.net/manual/en/function.array-push.php
Array_map: http://php.net/manual/en/function.array-map.php
实施例: https://gist.github.com/anonymous/d6749095ed20a995ec8697ef203d164f