Avatar of Refael
RefaelFlag for United States of America asked on

php form validation error messages

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!

<?php

   $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";
    }
?>

Open in new window

PHP

Avatar of undefined
Last Comment
Refael

8/22/2022 - Mon
Argenti

if (empty($yourVariable)) {
   // your code goes here
}

Open in new window


http://www.php.net/manual/en/function.empty.php
ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Refael

Hi Argenti,

I think you missed the questions.... please read again :-) Thank you anyway.
ASKER
Refael

Hello Ray_Paseur, The expert :-)

Well, I already have a client-side (javascript) validation. I simply wanted to know how to work with the loop on the required fields and setting the correct message per field.
I am sure your solution is 10 times better :-) I will have to find the time to learn/go through it. Thank you!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Ray Paseur

See if this makes sense...  Dealing with external input in PHP is a large topic unto itself!
http://php.net/manual/en/tutorial.forms.php
http://php.net/manual/en/language.variables.external.php
http://php.net/manual/en/security.intro.php

This is untested code; please use it as "advice" and not as an example of something that is suitable for your application work.

<?php // RAY_temp_refael.php
error_reporting(E_ALL);

// THE REQUIRED FIELDS ARE ARRAY KEYS IN THE POST REQUEST
$requiredFields = array('contactName', 'contactPhone');

// THE ERROR MESSAGES, IF ANY, WILL GO HERE
$err = array();

// USING OUR INTERNAL VARIABLES, CHECK THE POST ARRAY
foreach ($requiredFields as $key)
{
    if (empty($_POST[$key]))
    {
        $err[] = "Please Enter $key";
    }
    else
    {
        // IMPORTANT! SANITIZE THE EXTERNAL INPUT HERE (DO NOT JUST COPY IT!)
        $safe_post[$key] = $_POST[$key];
    }
}

// IF THERE ARE ANY OMISSIONS OR ERRORS, SHOW THE ERROR MESSAGES
if (!empty($err))
{
    foreach ($err as $msg)
    {
        echo PHP_EOL . $msg;
    }
    die('FAILURE');
}

// IF WE GET HERE, WE HAVE FOUND ALL OF THE FIELDS TO BE FILLED IN
// AND NOW WE CAN USE THE SANITIZED INFORMATION IN $safe_post
print_r($safe_post);

Open in new window

ASKER
Refael

Hi Ray_Paseur,

The above links and the example you wrote are a great lesson and i am sure many here in EE will enjoy it and will be thank you for it. I thank you too, again!