Avatar of janenh1955
janenh1955
 asked on

php form validation - highlight errors

Hi experts,

I need to validate this php form and highlight incorrect field names and inputs.
For validation i need required, numeric and minlength.

Thanks.


<form AUTOCOMPLETE="on" name="form_2b" method="post" id="form_2b" action="<?=$_SERVER['PHP_SELF']?>">

                        <tr >
                              
                              <td><label>Name:</label></td>
                              
                              <td><input type="text_input" name="name" id="name" style="WIDTH: 150px" value="<?php echo $_POST['name']?>"/>

                              </td>
                        </tr>
                        <tr >
                              
                              <td><label>Address:</label></td>
                              
                              <td><input type="text_input" name="address" id="address" style="WIDTH: 150px" value="<?php echo $_POST['address']?>"/>

                              </td>
                        </tr>
                        <tr >
                              
                              <td><label>Email:</label></td>
                              
                              <td><input type="text_input" name="email" id="email" style="WIDTH: 150px" value="<?php echo $_POST['email']?>"/>

                              </td>
                        </tr>
</form>
PHP

Avatar of undefined
Last Comment
janenh1955

8/22/2022 - Mon
Hugh McCurdy

IMHO, you haven't provided enough information in order to answer your inquiry.

The short answer is you need a php program that generates and then validates the form.  However, the question doesn't tell me anything about what makes a field valid or not or how the field should be highlighted.

I solved a similar problem in a different way but you can look at it to see if it has features you want.  My solution includes that I published the PHP code.

http://hugh.tekcities.com/hugh/program/web/phpFormSimple.php
Loganathan Natarajan

phpboopa

Rather than doing validation on server side (ie. PHP Server), Do validation on client side will be best solution to your requirement.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Olaf Doschke

It's much easier to influence html elements via embedded javascript, there are libraries, as logudotcom shows.

But actually you need to validate data within PHP anyway to prevent misuse by automation or bots etc. submitting forms.

You can limit the visual feedback of highlighting input controls to the javascript client side form check, as that is intended to help human users anyway. But the php script receiving the input should still check it and sanitze it for further processing, so you might then answer a submit with html including the coloring of input tags and putting back the input values, it's not hard to do either.

Bye, Olaf.
janenh1955

ASKER
sorry i don't understand what's more to tell about what i need. from my point of view it's a form that needs validation and highlight fields if error given. the file is form.php, input fields(yellow) and input names(red) should change color. as for the validation i nned it simple: fields must be required, numeric, and minlength. i guess that's all. please tell me what info do you need more.
janenh1955

ASKER
i need the file written. please don't give me examples i can't get them work.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Ray Paseur

I cannot write your code for you (at least not for free) but I can give you a well-commented example that uses PHP validation to highlight form errors.  Perhaps you can follow the design pattern.  Client-side validation is a convenience for your user community.  jQuery has many good tools for use in this sort of activity.  But jQuery relies on JavaScript and since attack scripts will simply bypass JavaScript and post data directly to your servers, you must do server-side validation as well, before your scripts accept any data.  The mantra is always, "accept only known good values!"

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


// DEMONSTRATE HOW TO HIGHLIGHT ERRORS IN FORM INPUT
// CLIENT IS ASKED TO PUT IN A VALUE
// IF THE VALUE FAILS OUR TEST WE SHOW AN ERROR MESSAGE
// WE PUT A MARKER NEXT TO THE INPUT CONTROL ON THE FORM
// WE TURN THE FORM BORDER RED
// SEE http://www.w3schools.com/CSS/pr_class_visibility.asp


// THESE CONDITIONS ARE SET FOR THE SCRIPT INITIALIZATION
$error_abc = 'hidden';
$boxer_abc = 'black';
$error_xyz = 'hidden';
$boxer_xyz = 'black';
$error_any = 'hidden';


// CAPTURE AND NORMALIZE THE POST VARIABLES - ADD YOUR OWN SANITY CHECKS HERE
$abc = (isset($_POST["abc"])) ? trim(strtoupper($_POST["abc"])) : NULL;
$xyz = (isset($_POST["xyz"])) ? trim(strtoupper($_POST["xyz"])) : NULL;

// IF ANYTHING WAS POSTED, VALIDATE IT
if (!empty($_POST))
{
    // VALIDATE THE 'abc' FIELD
    if ($abc != 'ABC')
    {
        $error_any = 'visible';
        $error_abc = 'visible';
        $boxer_abc = 'red';

        // BECAUSE THIS FAILED VALIDATION, REMOVE IT FROM THE FORM
        $abc       = NULL;
    }

    // VALIDATE THE 'xyz' FIELD
    if ($xyz != 'XYZ')
    {
        $error_any = 'visible';
        $error_xyz = 'visible';
        $boxer_xyz = 'red';

        // BECAUSE THIS FAILED VALIDATION, REMOVE IT FROM THE FORM
        $xyz       = NULL;
    }

    // DO WE HAVE INPUT FREE FROM ANY ERRORS?
    if ($error_any != 'visible')
    {
        echo "CONGRATULATIONS";
        die();
    }

    // OOPS - WE HAVE ERRORS AND MUST SHOW THE FORM AGAIN
}

// IF NOTHING WAS POSTED, OR IF THERE ARE ERRORS, WE NEED NEW CLIENT INPUT
$form = <<<ENDFORM
<style type="text/css" media="all">
.error_any { visibility:$error_any; }
.error_abc { visibility:$error_abc; }
.error_xyz { visibility:$error_xyz; }
</style>
<pre>
<form method="post">
<span class="error_any">PLEASE CORRECT THE FOLLOWING ERRORS</span>
<span class="error_abc">YOU MUST ENTER 'abc' IN THIS FIELD</span>
PLEASE ENTER "ABC" HERE: <input style="border-color:$boxer_abc;" name="abc" value="$abc" />
<span class="error_xyz">YOU MUST ENTER 'xyz' IN THIS FIELD</span>
PLEASE ENTER "XYZ" HERE: <input style="border-color:$boxer_xyz;" name="xyz" value="$xyz" />
<input type="submit" />
</form>
ENDFORM;

// WRITE THE FORM WITH THE APPROPRIATE CSS STYLES ON THE ERROR MESSAGE FIELDS
echo $form;

Open in new window

HTH, ~Ray
janenh1955

ASKER
i don;t understand it. told you i'm noob. if you can't write it, please give me simple code how to echo $req for each error field. i have this:

<td><label><? echo $req; ?>Email:</label></td>

and this on another file, verify.php:

$req = "<font size=3 color=red face=Verdana><b><sup>*</sup></b></font>";


anybody?
Hugh McCurdy

Janenh1955,

Are you trying to learn to use PHP or are you just trying to get a project done?  (What's the purpose of this adventure?)

~Hugh
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
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.
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
janenh1955

ASKER
it worked. thanks