I am trying to improve my form for the form validation. I do not display any result if the data is incorrectly submitted or when from all input fields even if one of them is missing. But I am displaying error message below like for input field like “email not correct format” and same for other fields (checkbox group and date field). I want to help the user get a better understanding of what they may have done wrong if their input is rejected. How shall I do this? I am thinking if any one input is wrong then I display message in detail and if all inputs are missing but I don’t understand how to do this.
Thanks in advance.
Here is my php file for validation:
<?php
// Global result of form validation
$valid = false;
// Global array of validation messages. For valid fields, message is ""
$val_messages = Array();
// Output the results if all fields are valid.
function the_results()
{
global $valid;
if($_SERVER["REQUEST_METHOD"]=="POST")
{
if($valid == true){
echo '<div class="results">'.'<div class="result-text">'."Your email address is ". $_POST['email'].'</div>'.'</div>';
//For checkboxes selected values
echo'<div class="results">'.'<div class="result-text">'."<ul>";
for($i=0;$i<count($_POST["animals"]);$i++){
echo "<li>{$_POST["animals"][$i]}</li>";
}
echo "</ul>".'</div>'.'</div>';
echo '<div class="results">'.'<div class="result-text">'."<p> Your favourite date is ". $_POST['date']. "</p>".'</div>'.'</div>';
} else{
echo " ";
}
}
}
// Check each field to make sure submitted data is valid. If no boxes are checked, isset() will return false
function validate()
{
global $valid;
global $val_messages;
// if email is valid, push "", else push "email is not valid"
if($_SERVER['REQUEST_METHOD']== 'POST')
{
// Use these patterns to check email and date, or come up with your own.
// email: '#^(.+)@([^\.].*)\.([a-z]{2,})$#'
// date: '#^\d{4}/((0[1-9])|(1[0-2]))/((0[1-9])|([12][0-9])|(3[01]))$#'
if(isset($_POST['email']) == true && isset($_POST['date']) == true){
$emailValid = false;
$dateValid = false;
$animalValid = false;
if(!preg_match('#^(.+)@([^\.].*)\.([a-z]{2,})$#', $_POST['email'])){
$emailValid = false;
$val_messages['email'] = "email not correct format";
}
else {
$emailValid=true;
$val_messages['email'] = "";
}
if(!preg_match('#^\d{4}/((0[1-9])|(1[0-2]))/((0[1-9])|([12][0-9])|(3[01]))$#', $_POST['date'])){
$dateValid = false;
$val_messages['date'] = "please enter a valid date in the format yyyy/mm/dd";
} else{
$dateValid = true;
$val_messages['date'] = "";
}
if(!empty($_POST['animals'])){
//Counting number of checked checkboxes
$checked_count = count($_POST['animals']);
if($checked_count >= 3){
$animalValid = true;
$val_messages['animals'] = "";
} else{
$animalValid = false;
$val_messages['animals'] = "please choose atleast three animals";
}
}
if(isset($_POST['animals']) == false){
$animalValid = false;
$val_messages['animals'] = "please choose atleast three animals";
}
if($emailValid == true && $dateValid == true && $animalValid == true){
$valid = true;
}
}
}
}
// Display error message if field not valid. Displays nothing if the field is valid.
function the_validation_message($type) {
global $val_messages;
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($type == 'email'){
echo "<div class = 'failure-message'>" .$val_messages['email']."</div>";
} else if($type == 'animals'){
echo "<div class = 'failure-message'>" .$val_messages['animals']. "</div>";
} else if($type == 'date'){
echo "<div class = 'failure-message'>" .$val_messages['date']. "</div>";
}
}
}