Hi I seem to be lost with this form validation

When submited empty the error should display next the input field in red.

Thanks in advance
D

<?php  
if (array_key_exists('submit',$_POST)){  
        //Form has been submitted 
            // Fields that are on form  
            $expected = array('name', 'email', 'comments');  
            // Set required fields  
            $required = array('name', 'comments');  
            // Initialize array for errors  
            $errors = array(); 

                foreach ($_POST as $field => $value){  
            // Make sure field was done correctly otherwise add error.  
        }  

        // Assign to $temp and trim spaces if not array  
        $temp = is_array($value) ? $value : trim($value);  

        // If field is empty and required, tag onto $errors array  
        if (empty($temp) && in_array($field, $required)) {  
              array_push($errors, $field);  

        } 

        if (empty($errors)){  
                //The form is completed properly to do with as you please  
                unset($errors);  
        }

}
?> 

<html>  
    <head>  
        <title>Form</title>  
        <link rel="stylesheet" type="text/css" href="assets/css/default.css">  
    </head>  
    <body>  
        <div id="content">  

                <form id="commentform" method="post" action="index.php">  
                    <p>  
                    <label for="name">Name:</label>  <?php if (isset($errors) && in_array('name', $errors)){?> <div class="red">Please enter name</div>  <?php } ?>  
                    <br/>  
                   <input name="name" id="name" type="text"  <?php if (isset($errors)) { echo 'value="'.htmlentities($_POST['name']).'"'; } ?>  />

                    </p>  
                    <p>  
                    <label for="email">Email:</label> <br/>  
                    <input name="email" id="email" type="text"/>  
                    </p>  
                    <p>  
                    <label for="comments">Comment:</label>  
                    <br/>  
                    <textarea name="comments" id="comments" rows="4"><?php if (isset($errors)) { echo htmlentities($_POST['comments']); }  ?>

The easiest way is by jQuery.

When the type is released, the following details will be saved in the $_POST comprehensive variety (or $_GET comprehensive variety based on the form’s technique attribute). The in the left-hand comprehensive variety are taken from the control’s name operate.

In your php script, you can use the code;

<?php
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];

if (empty($fname)) { 
     $errorfname= '<div class="isa_error">Frist name is required</div>'; 
} 
if (empty($lname)) { 
     $errorlname = '<div class="isa_error">Last name is required</div>'; 
} 
?>

And in your form, you can write the code;

<html>
<body>
<form action="" method="post">
<input type="text" name="firstname">
<?php if(isset($errorfname)) { echo $errorfname; } ?> // fname error message
<input type="text" name="lastname">
 <?php if(isset($errorlname)) { echo $errorlname; } ?> //lname error message
<input type="submit" name="submit" value="Submit">
</form>

That should work

Thanks very much for your help Webville312 i did come up with a solution.

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim($_POST["name"]);
    $email = trim($_POST["email"]);
    $message = trim($_POST["message"]);


    if ($name == "" OR $email == "" OR $message == "") {
        $error_message = "You must specify a value for name, email address, and message.";
       }

    foreach( $_POST as $value ){
        if( stripos($value,'Content-Type:') !== FALSE ){
            $error_message = "There was a problem with the information you entered.";    

        }
    }

    if ($_POST["address"] != "") {
        $error_message = "Your form submission has an error.";

    }

    require_once("inc/phpmailer/class.phpmailer.php");
    $mail = new PHPMailer();

    if (!$mail->ValidateAddress($email)){
        $error_message = "You must specify a valid email address.";

    }

Thanks
D

Just a usability note: "Your form submission has an error." is kind of an unclear error message. Do you really want the user to read such a thing? He might get confused ;).

Yo most welcome. Could you please mark this thread as solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.