First of all I would like to thank Codecourse for the tutorial. Everything works a treat but i would like to display the errors into my table td. Maybe a dumb question but I'm a beginner and tried working it out my self with no luck
<?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>
Like this:
<?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>
Just store them in a variable, and then output them where you want, pretty straight forward.
Push all of the errors to an array, and then display all of those errors in your table.
Array_push: http://php.net/manual/en/function.array-push.php
Array_map: http://php.net/manual/en/function.array-map.php
Example: https://gist.github.com/anonymous/d6749095ed20a995ec8697ef203d164f
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.