Link to home
Start Free TrialLog in
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

Avatar of Argenti
Argenti
Flag of France image

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

Open in new window


http://www.php.net/manual/en/function.empty.php
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Refael

ASKER

Hi Argenti,

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

ASKER

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!
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

Avatar of Refael

ASKER

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!