[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.8

php form validation: highlight incorrect fields

Asked by robjmills in PHP Scripting Language

Tags: address, boxes, form, highlight, validate

I'm making a basic user registration for a mailing list form for a website i'm working on. I've made the form and implemented basic error checking functionality, however i want this to be amended. At the moment when an error occurs, through a user failing to complete a field, it is added to an array of errors called errorList, when this has any content these errrors are echoed bqack to the user. It would be preferable however if any errors on behalf of the user mirror back the form they have submitted with correct values retained and incorrect form fields highlighted by adding a red outline to the input boxes and the label for the input box to also be highlighed. When these inputs are checked, rather than just checking if they are completed i would like to perform checks related to the particular inputs, e.g. checking if the email address is of a valid construct, and checking if the postcode (UK equivalent to ZIP code) is valid and contains only letters and numbers.

I'm using css to style my site and i know that the best way to add color to the error input boxes would be by adding an error class, i just dont know how i would implement this.

I'm using a functions page for my site as a template for the design, this is called functions.php and is called at the top of the script, pagetop() and pagebottom() are the functions that display the top and bottom half of the page. db.php is used to call my database connection to my mysql database where the correct data will be inputted.

This is what i've got so far:

<?php
require 'functions.php';
include 'db.php';

$pagename = "SUBSCRIBE";

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$house_number = $_POST['house_number'];
$street = $_POST['street'];
$town = $_POST['town'];
$postcode = $_POST['postcode'];
$county = $_POST['county'];
$submit = $_POST['submit'];

pagetop($pagename);

if (!$submit)
            {
            echo '
            If you are interested in further details please complete this form</a>
            ';
            
            ?>
            <br><br>
            <form method="post" action="<? echo $PHP_SELF; ?>">
            <table width="75%" cellpadding="4">
                  <tr>
                        <td>Name:</td>
                        <td><input type="text" name="name" id="name"></td>
                  </tr>
                  <tr>
                        <td>Email:</td>
                        <td><input type="text" name="email" id="email"></td>
                  </tr>
                  <tr>
                        <td>Telephone Number:</td>
                        <td><input type="text" name="phone" id="phone"></td>
                  </tr>
                  <tr>
                        <td>House Name/Number:</td>
                        <td><input type="text" name="house_number" id="house_number"></td>
                  </tr>
                  <tr>
                        <td>Street:</td>
                        <td><input type="text" name="street" id="street"></td>
                  </tr>
                  <tr>
                        <td>Town:</td>
                        <td><input type="text" name="town" id="town"></td>
                  </tr>
                  <tr>
                        <td>Postocde:</td>
                        <td><input type="text" name="postcode" id="postcode"></td>
                  </tr>
                  <tr>
                        <td>County:</td>
                        <td><input type="text" name="county" id="county"></td>
                  </tr>
                  <tr>
                        <td>&nbsp;</td>
                        <td><input type="Submit" name="submit" value="Add"></td>
                  </tr>
            </table>
            </form>
            <?
            }
            else
            {
                  // set up error list array
                  $errorList = array();
                  $count = 0;
                  
                  // validate text input fields
                  if (!$name) {
                                    $errorList[$count] = "Invalid entry: Name"; $count++;
                                    }
                  
                  if (!$email) {
                                          $errorList[$count] = "Invalid entry: Email"; $count++;
                                          }
                  
                  if (!$phone)
                                          {
                                          $errorList[$count] = "Invalid entry: phone"; $count++;
                                          }
                                          
                  if (!$house_number)
                                          {
                                          $errorList[$count] = "Invalid entry: House Number"; $count++;
                                          }
                                          
                  if (!$street)
                                          {
                                          $errorList[$count] = "Invalid entry: Street"; $count++;
                                          }
                                          
                  if (!$town)
                                          {
                                          $errorList[$count] = "Invalid entry: Town"; $count++;
                                          }
                                          
                                          
                  if (!$postcode)
                                          {
                                          $errorList[$count] = "Invalid entry: postcode"; $count++;
                                          }
                  
                  if (!$county)
                                          {
                                          $errorList[$count] = "Invalid entry: county"; $count++;
                                          }
                                          
                                          // check for errors
                  // if none found...
                  if (sizeof($errorList) == 0)
                  {
                        // generate and execute query
                        $query = "INSERT INTO subscription(name, email, phone, house_number, street, town, postcode, county) VALUES('$name', '$email', '$phone', '$house_number', '$street', '$town', '$postcode', '$county' )";
                        $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
            
                        // print result
                        echo "<font size=-1>Insert Successful. <a href=index.php>Go back to the main menu</a>.</font>";
            
                  }
                  else
                  {
                        // errors found
                        // print as list
                        echo "<font size=-1>The following errors were encountered: <br>";
                        echo "<ul>";
                        for ($x=0; $x<sizeof($errorList); $x++)
                        {
                              echo "<li>$errorList[$x]";
                        }
                        echo "</ul></font>";
                        echo '<a href="javascript:history.back()">Return to subscription page</a>';
                  }
            }

?>
<?

pagebottom()

?>

Thanks a lot.
 
Related Solutions
Keywords: php form validation: highlight incorrect…
 
Loading Advertisement...
 
[+][-]11/07/03 04:27 AM, ID: 9700638Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: PHP Scripting Language
Tags: address, boxes, form, highlight, validate
Sign Up Now!
Solution Provided By: seengee
Participating Experts: 5
Solution Grade: B
 
[+][-]10/30/03 10:52 AM, ID: 9652353Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/30/03 11:08 AM, ID: 9652484Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/30/03 02:00 PM, ID: 9653751Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/30/03 09:29 PM, ID: 9655562Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/31/03 02:07 AM, ID: 9656486Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/31/03 02:08 AM, ID: 9656491Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92