troubleshooting Question

php form validation error messages

Avatar of Refael
RefaelFlag for United States of America asked on
6 Comments1 Solution430 ViewsLast Modified:
Hello Experts,

Do not get me wrong that's not how i validate my forms :-) but... i know how to echo an error message per form field. Yet i am trying to understand how this is possible to do this using the "foreach" loop and then echo an error message per field in case it is empty. Thank you so much!


   $contactName = htmlentities(trim($_POST["contactName"]));
   $contactPhone = htmlentities(trim($_POST["contactPhone"]));

   $requiredFields = array('$contactName', '$contactPhone');
   foreach ($requiredFields as $requiredField) {
     if ($requiredField === '') {

    ///////// if the $contactName is left empty then
     $err_contactName = "please enter your name";

   //////// if the $contactPhone is left empty then

     $err_contactPhone = "please enter your phone";
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros