Hi all,
I am trying to create a function that basically allows me to validate form fields.
Simple the process is I use an array to specify which form fields are required, and then I check the $_POST array against that array... here is the code I have now:
$required_fields = array('name', 'email', 'message');
$error_msg = ''; //used to output error
//validate form
foreach($_POST as $key => $value)
{
//if form field does not exist in the required array
if(!in_array($key, $required_fields))
{
$error_msg .= '';
$RETURN = 0;
}
else
{
if(empty($value))
{
$error_msg .= ucfirst($key) . ', ';
$RETURN = 0;
}
else
{
$RETURN = 1;
}
}
}
Maybe there is a simpler/more efficient way to accomplish this? This script is supposed to check if the form fields required are filled out... if they are not, it should add value to the $error_msg variable ("FIELD is empty, <br>").
Any help would be appreciated... thanks.