0

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>
7
  • Just move the foreach looping over the errors down to where you want to show them. Commented Jul 29, 2016 at 20:05
  • Assign all the errors to a variable ($error_messages) and echo it where ever you want. Commented Jul 29, 2016 at 20:06
  • @JimL - I don't think it is that simple, we'll have to add the errors in that loop to an array, and then do an additional foreach loop Commented Jul 29, 2016 at 20:08
  • @ArtisiticPhoenix why? I haven't seen the full code / codecourse tutorial, but after what's posted here it seems like it would be no problem just moving the loop. Commented Jul 29, 2016 at 20:10
  • @JimL - mainly because you would have to check that the if conditions pass to create the $validate object. So you would be doing additional and unnessacery logical checks. It also makes the errors less portable, because you are relying on their source, for example, you couldn't inject a message in there anywhere else if you wanted to. Commented Jul 29, 2016 at 20:14

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.