Hello, how can I improve this form login with error alerts?
For example, if the fields are empty, if entered data do not match any user, etc. etc.

Right now my form is so, taking advantage of a class:


    //Retrieve the field values from our login form.
    $username = !empty($_POST['username']) ? trim($_POST['username']) : null;
    $password = !empty($_POST['password']) ? trim($_POST['password']) : null;

    if($user->login($username, $password)) {
        header('Location: index.php');
    } else {
        $message[] = 'We found problems with the login, please try again.';

I would suggest you use javascript/jquery to validate data if null etc..
trim the input so no spaces in username and password before validating at javascript

and if all valid send to server.. when data arrives at server check username exists.. if not add error in an array..

and loop through the error array and echo each of them out..

Member Avatar

JS validation is fine as window dressing, but data validation must also happen on the server. JS validation can be by-passed very easily.

The passing of error info to an array is fine, but you need to be able to access that data in the new page. One way would be to place it in a session variable - and delete it from the session once displayed. Else you could pass an error code in the url:

$_SESSION['form_errors'] = $message;


header('Location: index.php?err=64');