Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9053909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:32:41+00:00 2026-06-16T13:32:41+00:00

I am trying to create error messages if certain conditions aren’t met. So the

  • 0

I am trying to create error messages if certain conditions aren’t met. So the user fills out a form and if a field is empty or doesn’t pass my validation it returns the error message.

This is the form:

if (isset($_POST)) {
    if (checkEmail($email) == TRUE && $name != NULL && $surName != NULL) {    
        mysql_query(    "INSERT INTO USR_INFO (NAME, MAIL, SURNAME) 
                         VALUES ('$name', '$email','$surName') ") or die(mysql_error());
        header('Location: thanks.php');
    } 

    else {
        echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">            
                <label for="name">First Name</label>
                <input type="text" name="name" id="name" value="' .$_POST['name'].'" />
                <span class="required">&#42;</span>

                <label for="surName">Last Name</label>
                <input type="text" name="surName" id="surName" value="' .$_POST['surName']. '" />
                <span class="required">&#42;</span>

                <label for="email">E-mail</label>
                <input type="email" id="email" name="email" placeholder="example@domain.com" value="' .$_POST['email']. '" />
                <span class="required">&#42;</span>

                <input type="submit" name="submit" id="submit">
            </form>';
    }
} else {
        echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">            
                <label for="name">First Name</label>
                <input type="text" name="name" id="name" value="" />
                <span class="required">&#42;</span>

                <label for="surName">Last Name</label>
                <input type="text" name="surName" id="surName" value="" />
                <span class="required">&#42;</span>

                <label for="email">E-mail</label>
                <input type="email" id="email" name="email" placeholder="example@domain.com" value="" />
                <span class="required">&#42;</span>


                <input type="submit" name="submit" id="submit">
            </form>';
}

So what I tried is adding an array to display the error messages like so:

$errorMessage = array();

And add this to the html form field with the proper message:

$error[] = "Error Message";

Now what I am stuck with is that I want to have the error show only if a user doesn’t meet the conditions

if ($name == NULL) {$error[] = "Error Message";}
if ($surName == NULL) {$error[] = "Error Message 2";}
if (checkEmail($email) == FALSE || NULL) {$error[] = "Error Message 3";}

But I can’t make it work. When I tried to implement this logic it will parse the page fine and the validation works as well but the error messages wont show up if I leave a required field blank. My guess is that I didn’t loop through it properly.

Help is much appreciated!

EDIT:

I tried the answer that was posted by Frosty Z and this is what I have at the moment:

    if (isset($_POST)) {
    $errorMessage = array();
        if ($name == '') { $errors[] = "Input name please." }
        if ($surName == '') { $errors[] = "Input last name please." }
        if (!checkEmail($email)) { $errors[] = "Email address not valid." }

    if (count($error) == 0) { 
            mysql_query(    "INSERT INTO USR_INFO (NAME, MAIL, SURNAME) 
                             VALUES ('$name', '$email', '$surName') ") or die(mysql_error());
            header('Location: thanks.php');
            exit;
    else {

        if (count($errors) > 0)
            echo "<p>Sorry, there are problems with the information you have provided:</p>";

        foreach($errors as $error)
            echo '<p class="error">'.$error.'</p>';

        echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">            
                <label for="name">Name</label>
                <input type="text" name="name" id="name" value="' .$_POST['name'].'" />
                <span class="required">&#42;</span>

                <label for="surName">Last name</label>
                <input type="text" name="surName" id="surName" value="' .$_POST['surName']. '" />
                <span class="required">&#42;</span>

                <label for="email">E-mail</label>
                <input type="email" id="email" name="email" placeholder="example@domain.com" value="' .$_POST['email']. '" />
                <span class="required">&#42;</span>

                <input type="submit" name="submit" id="submit">
            </form>';
    }
} else {
        echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">            
                <label for="name">Name</label>
                <input type="text" name="name" id="name" value="" />
                <span class="required">&#42;</span>

                <label for="surName">Achternaam</label>
                <input type="text" name="surName" id="surName" value="" />
                <span class="required">&#42;</span>

                <label for="email">E-mail</label>
                <input type="email" id="email" name="email" placeholder="example@domain.com" value="" />
                <span class="required">&#42;</span>

                <input type="submit" name="submit" id="submit">
            </form>';
}

With this my page won’t be parsed. I have error reporting on but it doesn’t show anything besides a

Internal server error 500

in my console log(Firebug)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:32:43+00:00Added an answer on June 16, 2026 at 1:32 pm

    Here is some rewriting of your work with a minimal handling of error messages.

    BTW, you should consider adopting a decent PHP framework which will help you to handle a lot of common development tasks.

    $name = '';
    $surName = '';
    $email = '';
    
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
        $name = $_POST['name'];
        $surName = $_POST['surName'];
        $email = $_POST['email'];
    
        $errors = array();
    
        if ($name == '') { $errors[] = "Please type your name."; }
        if ($surName == '') { $errors[] = "Please type your surname."; }
        if (!checkEmail($email)) { $errors[] = "Wrong email format."; }
    
        if (count($errors) == 0) {
            // tip: use PDO or mysqli functions instead of mysql ones to bind variables.
            // currently there is a risk of SQL injection here
            mysql_query("INSERT INTO USR_INFO (NAME, MAIL, SURNAME) 
                         VALUES ('$name', '$email','$surName') ") or die(mysql_error());
            header('Location: thanks.php');
            exit;
        }
    }
    
    if (count($errors) > 0)
        echo '<p>Sorry, there are problems with the information you have provided:</p>';
    
    foreach($errors as $error)
        echo '<p class="error">'.$error.'</p>';
    
    echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">            
                <label for="name">First Name</label>
                <input type="text" name="name" id="name" value="'.htmlspecialchars($name).'" />
                <span class="required">&#42;</span>
    
                <label for="surName">Last Name</label>
                <input type="text" name="surName" id="surName" value="'.htmlspecialchars($surName).'" />
                <span class="required">&#42;</span>
    
                <label for="email">E-mail</label>
                <input type="email" id="email" name="email" placeholder="example@domain.com" value="'.htmlspecialchars($email).'" />
                <span class="required">&#42;</span>
    
                <input type="submit" name="submit" id="submit">
            </form>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to create a form that submits to a certain action within the
I am trying to create an upload form that allows the user to select
Trying to create a background-image slideshow and am getting this error... This is the
So trying to create a rest service but I keep getting an error: If
I am trying to create three tables however I am getting error as You
I am trying to create full text index but I am getting error which
I am trying to create a 2 dimensional array but am getting this error.
I'm trying to create a database. But i get an error while i'm doing.
I'm having troubles with a syntax error when trying to create a function in
I'm trying the following: CREATE TABLE Table1 ( RecordNo autonumber, --error here! PersonId varchar(50),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.