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 1053655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:17:14+00:00 2026-05-16T17:17:14+00:00

How can I populate and display the $errors variable only after the form is

  • 0

How can I populate and display the $errors variable only after the form is submitted and there is an error?

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
  <?php
    if(!is_null($errors)) {
        echo "<h3>$errors</h3>";
    }
  ?>

  <label for="Name">Name</label><em>(Required)</em>
  <input type="text" name="Name"/>

  <label for="Email">Email</label><em>(Required)</em>
  <input type="text" name="Email"/>

  <label for="Website">Website</label>
  <input type="text" name="Website"/>

  <label for="Message">Message</label><em>(Required)</em>
  <textarea name="Message"></textarea>

  <button id="submit" type="submit" value="submit"name="submit">Submit</button>
  <input type="hidden" name="Submit" />
</form>

and my script to handle the form

$errors = null;

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

  if(!empty($_POST['Name']) && !empty($_POST['Email']) && !empty($_POST['Message'])) {

    $name = $_POST['Name'];
    $email = $_POST['Email'];
    $website = $_POST['Website'];
    $message = $_POST['Message'];

    //strip any html tags and slashes off of the fields
    $nameClean = strip_tags($name);
    $emailClean = strip_tags($email);
    $websiteClean = strip_tags($website);
    $messageClean = strip_tags($message);
    $messageCleanAndStripped = stripslashes($messageClean);
  }

} else {
    $errors = "Please fill in the required fields";
}
  • 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-05-16T17:17:14+00:00Added an answer on May 16, 2026 at 5:17 pm

    $errors will be displayed even when you load the page for the first time, because that’s what your code does:

    if(isset($_POST['submit'])) 
    {
      ....
    } 
    else 
    {
        $errors = "Please fill in the required fields";
    }
    

    It means if the user loads the page the first time, he hasn’t posted anything, so isset($_POST[‘submit’]) is false, then you assign an error message to the $errors variable. You should change your code to this:

    if(isset($_POST['submit'])) {   
    
      if(!empty($_POST['Name']) && !empty($_POST['Email']) && !empty($_POST['Message'])) {
    
        $name = $_POST['Name'];
        $email = $_POST['Email'];
        $website = $_POST['Website'];
        $message = $_POST['Message'];
    
        //strip any html tags and slashes off of the fields
        $nameClean = strip_tags($name);
        $emailClean = strip_tags($email);
        $websiteClean = strip_tags($website);
        $messageClean = strip_tags($message);
        $messageCleanAndStripped = stripslashes($messageClean);
      }
      else 
      {
        $errors = "Please fill in the required fields";
      }
    
    } 
    

    Just insert the else statement inside the condition, so you’ll be checking for errors only when the user actually submitted something.

    Hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get the variable My.Application.Info.Version.ToString to populate in the comments section? Dim
How can I do this in Javascript/jQuery from PHP? I am trying to populate
Can you populate a set from a soql query? I couldn't get my syntax
How can I populate multiple datagrids in flex with a single datasource that is
How can i populate an array in loop? I'd like to do something like
I have a requirement where I can use JS Fiddle dropdown to populate States
i have a form and a sortable() list where a user can drag from
In my MVC application for booking accommodation I have the following: Action to display
I can see that my ModelState.Values[x].Errors is correctly populated with the two validation errors
I'm trying to use a php function within a template html form that is

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.