Link to home
Start Free TrialLog in
Avatar of damianb123
damianb123Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Help setting textbox border colour on validation failure

Hi,
    I am using a PHP page with some validation code as follows:

      if (!empty($_POST['street'])) {
            $street = $_POST['street'];
        } else {
            echo '<p class="errorlarger"><strong>Please enter your street</strong></p>';
        }

However rather than displaying a string of text, I want to change the textbox border of the field causing an issue, how would I change this line of PHP to allow for the textbox border to be changed?

echo '<p class="errorlarger"><strong>Please enter your street</strong></p>';

Thx in advance.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

This teaching example script does not change the border color, but it uses CSS to hide or expose the error messages.  Hopefully you can modify it to change the CSS for your purposes.  You can test it on my server here:
http://www.laprbass.com/RAY_form_highlight_errors.php

HGH, ~Ray
<?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
// SEE http://www.w3schools.com/CSS/pr_class_visibility.asp


// THESE CONDITIONS ARE SET FOR THE INITIAL ENTRY
$error_any = 'hidden';
$error_abc = 'hidden';
$error_xyz = 'hidden';

// CAPTURE AND NORMALIZE THE POST VARIABLES, IF ANY
$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';
        $abc       = NULL;
    }

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

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

// 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 name="abc" value="$abc" />
<span class="error_xyz">YOU MUST ENTER 'xyz' IN THIS FIELD</span>
PLEASE ENTER "XYZ" HERE: <input 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

If you make a Google search for CSS Forms Border Color, you will find a lot of good resources.  This looked good at first glance:
http://webdesign.about.com/cs/forms/a/aaprettyforms.htm
Avatar of damianb123

ASKER

Hi Ray,
     thanks for your input, however I really need someone to provide the content to integrate with PHP, I could have looked myself if I had the time, hence the post with 500 points on here.

Sorry.
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
Hi,
    I've tried to make this work but cannot...... surely you need some form of PHP wrapper here to make the variable appear:

<input style="border-color:$boxer_abc;" name="abc

html will just see this as plain text?

Damian
Please try clicking this link.  Experiment with the script.  It is on my server and you can see exactly how it works.  The source code is posted at ID:35342262.
http://www.laprbass.com/RAY_form_highlight_errors.php

Best regards, ~Ray
Thanks, this worked fine in the end.

Cheers
Thanks for the points - it's a great question! ~Ray