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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:45:54+00:00 2026-06-06T18:45:54+00:00

I am working on a simple contact form written in php and have almost

  • 0

I am working on a simple contact form written in php and have almost everything setup as I would like for it to be except right now, all validation errors are displaying in a div on the top of my form, and I would like to change the code below so they are displayed next to the form field that caused the error, but I am not sure how to accomplish this so I have come to the experts for some advise.

PHP Validation Routine

$frm_valid = new Validate();

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

$rules=array(
    array('Type'=>'Required','Var'=>$_POST['name'],'Msg'=>'Name field is required.'),
    array('Type'=>'Required','Var'=>$_POST['email'],'Msg'=>'Email address field is required.'),
    array('Type'=>'Email','Var'=>$_POST['email'],'Msg'=>'Your email address format is invalid.'),
    array('Type'=>'Required','Var'=>$_POST['subject'],'Msg'=>'Subject field is required.'),
    array('Type'=>'Required','Var'=>$_POST['message'],'Msg'=>'Message field is required.'),
);


$result = $frm_valid->Valid($rules);

if(is_array($result)){
    // Print validation errors (if any)
    echo('<div"><b>The form cannot be submitted until the following errors are corrected.</b><ul>');

    foreach($result as $key=>$val){
      echo '<li>'.$val.'</li>';
    }
    echo('</ul></div><br />');
  } 
  else {
    // Do something else.
  }
}

Forms HTML

<form action="<? echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <input type="text" name="name" value="" /> <br />

    <input type="text" name="email" value="" />  <br />

    <input type="text" name="subject" value="" /> <br />

    <textarea name="message" cols="80" rows="7"></textarea> <br />

    <input type="submit" name="submit" value="Send" />
</form>
  • 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-06T18:45:56+00:00Added an answer on June 6, 2026 at 6:45 pm

    You need to change the whole code for this!!! The structure, perhaps this way:

    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
        <ul>
            <li>
                <label>
                    <span>Name</span>
                    <input type="text" name="name" />
                    <small class="errorText"><?php echo ($_POST["name"] == "") ? "This field is required" : ""; ?></small>
                </label>
            </li>
            <li>
                <label>
                    <span>Email</span>
                    <input type="text" name="email" />
                    <small class="errorText"><?php echo ($_POST["email"] == "") ? "This field is required" : ""; ?></small>
                </label>
            </li>
            <li>
                <label>
                    <span>Subject</span>
                    <input type="text" name="subject" />
                    <small class="errorText"><?php echo ($_POST["subject"] == "") ? "This field is required" : ""; ?></small>
                </label>
            </li>
            <li>
                <label>
                    <span>Message</span>
                    <textarea name="message" cols="80" rows="7"></textarea>
                    <small class="errorText"><?php echo ($_POST["message"] == "") ? "This field is required" : ""; ?></small>
                </label>
            </li>
            <li>
                <input type="submit" name="submit" value="Send" />
            </li>
        </ul>
    </form>
    

    And the CSS for the same:

    * {font-family: Segoe UI, Tahoma;}
    h1 {font-weight: bold; font-size: 14pt; padding: 5px 0; margin: 5px 0; border: 1px solid #999; border-width: 1px 0;}
    input[type='submit'] {padding: 5px 20px; cursor: pointer;}
    ul, li {display: block; list-style: none; margin: 0; padding: 0;}
    ul li {padding: 5px 0;}
    ul li label span {display: block; cursor: pointer;}
    ul li label .errorText {color: #f00; font-weight: bold; vertical-align: top;}
    ul li label textarea {width: 300px;}
    

    You can see a live demo here: Demo

    Error Handling in PHP

    Keep an error variable for each field. Say,

    <?php
        $error = array(
            "name" => "",
            "email" => "",
            "subject" => "",
            "message" => ""
        );
    ?>
    

    Update them with the errors and display them below.

    <?php
        if (empty()$_POST["email"])
            $error["email"] = "Email is required!";
        elseif (!isEmail($_POST["email"]))
            $error["email"] = "Not a Valid Email!";
    ?>
    

    If there are no errors, it would be empty and the user doesn’t see the error message. In your Forms Code, you need to just update this way:

    <small class="errorText"><?php echo $error["name"]; ?></small>
    <small class="errorText"><?php echo $error["email"]; ?></small>
    

    where in the backend, the $error["email"] would have either "This field is required" or "Not a valid email address!".

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

Sidebar

Related Questions

i have developed a simple php contact form, that's working fine, but i am
I had this simple modal contact form working fine but must have broke it
having a problem getting my simple php email contact form working. No errors are
I'm working with my simple PHP captcha algorithm ( http://www.source.ofitall.com/devel/captcha.php ) and I have
Ok I have our contact form fully working, and processing correctly. I have two
I am working on simple form to validate fields like this one. public class
I am working on a simple messenger using JSF and PrimeFaces. I would like
I've got a basic PHP contact form and have it process.php file that checks
im working on a simple game like space invaders,and i got into a problem.
Have created simple Ajax enabled contact forms before that have around 12 fields -

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.