1

I have the following code :

    if(isset($_POST['submit'])){

        if (! isset($_POST['firstname'])) {
            $error[] = "Please fill out all fields";
        }

        if (! isset($_POST['surname'])) {
            $error[] = "Please fill out all fields";
        }
........

with validation:

if (strlen($_POST['firstname']) < 2){
        $error[] = 'First name cannot be empty';
    }
    if (strlen($_POST['surname']) < 2){
        $error[] = 'Please provide your surname';
    }

......

More checks are made with the database....

This checks for errors and displays them in one go:

if(isset($error)){
                    foreach($error as $error){
                        echo '<p class="error-login">'.$error.'</p>';
                    }
                }

While this is working fine, I would like errors to be shown under each input box where there is an error happening.

I don't want to change the entire code, just want to make the necessary changes to this one, which I am incapable of doing myself.

Is putting them in array the only approach here or is there a simpler way ?

Thanks.

1
  • Add a key to errors: $error['firstname'] = '...'; and under each input check for errors under key. Commented Apr 6, 2020 at 11:03

1 Answer 1

1

The approach is - add errors to $error under a certain key, I presume - name of the input field:

if(isset($_POST['submit'])){

    // I use key `all` for errors that don't belong to any field

    if (! isset($_POST['firstname'])) {
        $error['all'] = "Please fill out all fields";
    }

    if (! isset($_POST['surname'])) {
        $error['all'] = "Please fill out all fields";
    }

    if (strlen($_POST['surname']) < 2){
        $error['surname'] = 'Please provide your surname';
    }

In your html markup:

// general errors, not related to inputs
if(isset($error['all'])){
    foreach($error['all'] as $err){
        echo '<p class="error-login">'.$err.'</p>';
    }
}

<input type="text" name="surname" />
<?php
if(isset($error['surname'])){
    foreach($error['surname'] as $err){
        echo '<p class="error-login">'.$err.'</p>';
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Looks like the best approach for that question.
Thanks, I appreciate your response however, to my code, I've made every modification as pointed out but it's not working. The form when submitted shows no errors and even when I fill out all fields it does not submit. I thought it might be the database processing as it returns this "$error[] = $e->getMessage();" but I've removed email & password and still the same. Thought it might be input values when returning "<?php if(isset($error)){ echo htmlspecialchars($_POST['firstname'], ENT_QUOTES); } ?>" but I've changed these to "if(isset($error['firstname'])" and still nothing. Any ideas ?

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.