Jump to content

[SOLVED] Help using more descriptive names in error message


tqla

Recommended Posts

Hello. My code displays error messages by field name but I would like then to display in a more descriptive name. I have created the $labels = array but I can't figure out how to use it. The errors are still showing the field name.

 

Here is the pertinent part of my script. Please help. Thanks!

 

<?php require_once('Connections/Auth.php'); ?>
<?php
  session_start();  

   case "new":   
   
     /* set up array containing all the fields */
  $labels = array ( "newname" => "User Name",
                    "newpass" => "Password",
                    "firstname" => "First Name",
                    "lastname" => "Last Name",
                    "company" => "Company",
                    "phone" => "Phone",
                    "email" => "Email",
                    "zip" => "Zip Code",		
				);
   
                                                

  /* Check for blanks */                           
     foreach($_POST as $field => $value)               
     {
        if ($field != "fax")                           
        {
           if ($value == "")                           
           {
              $blanks[] = $field;
           }
        }
     }
     if(isset($blanks))                               
     {
        $message_new = "<b>The following fields are blank.</b><br><br>  
            Please enter the required information:  ";
        foreach($blanks as $value)
        {
           $message_new .= "<BR><b>$value</b>";
        }
        extract($_POST);
        include("formSRL.inc");
        exit();
     }

    /* Validate data */
     foreach($_POST as $field => $value)              
     {
        if(!empty($value))                            
        {
           if(eregi("newname",$field) and
              !eregi("newpass",$field))
           {
              if (!ereg("^[A-Za-z' -]{1,50}$",$value)) 
              {
                 $errors[]="$value is not a valid name."; 
              }
           }
           if(eregi("firstName",$field) or eregi("lastName",$field) or eregi("company",$field) or eregi("title",$field))
           {
              if(!ereg("^[A-Za-z0-9.,'& -]{1,50}$",$value))
              {
                 $errors[] = "$value is not a valid.";
              }
           }
           if(eregi("email",$field))
           {
              if(!ereg("^.+@.+\\..+$",$value))
              {
                 $errors[] = "$value is not a valid 
                              email address.";
              }
           }
           if(eregi("phone",$field))
           {
              if(!ereg("^[0-9)(xX -]{7,20}$",$value))
              {
                 $errors[] = "$value is not a valid  
                              phone number. ";
              }
           }
        } // end if empty                             
     } // end foreach
     if(@is_array($errors))                           
     {
        $message_new = "";
        foreach($errors as $value)
        {
          $message_new .= $value." Please try 
                                   again<br />";
        }
        extract($_POST);
        include("formSRL.inc");
        exit();
     } 

      /* clean data */
$connection = mysql_connect($hostname_Auth, $username_Auth, $password_Auth) 
or die ("Couldn't connect to server.");
$db = mysql_select_db($database_Auth, $connection)
or die ("Couldn't select database."); 

     foreach($_POST as $field => $value)              
     {
        if($field != "Button" and $field != "do")
        {
           if($field == "password")
           {
              $password = strip_tags(trim($value));
           }
           else
           {
              $fields[]=$field;
              $value = str_replace("&", "and", strip_tags(trim($value)));
              $values[] = mysql_real_escape_string($connection,$value);
              $$field = $value;                 
           }
        }
     }


                                                  
      /* check to see if login name already exists */

      $sql = "SELECT loginName FROM Member 
                WHERE loginName='$newname'";
      $result = mysql_query($sql)
                or die(mysql_error());
      $num = mysql_numrows($result);
      if ($num > 0)                                      
      {
        unset($_GET['do']);
        $message_new = "<p><font color=#ff0000>$newname already used. Select another
                         Username.</font></p>";
        include("formSRL.inc");
        exit();
      }
      else                                               
      { 
?>

You could change

        if ($field != "fax")                           
        {
           if ($value == "")                           
           {
              $blanks[] = $field;
           }
        }

to

        if ($field != "fax")                           
        {
           if ($value == "")                           
           {
              $blanks[] = $labels[$field];
           }
        }

Well, try to echo some information just before the foreach and inside the foreach - perhaps a couple of other places as well. In that way you can see when the execution stops and then you'll know where to look for the problem.

 

I have a couple of suggestions for your code though: Inside the foreach you're using regular expressions to match a string, it would be much more efficient to just the comparison operators or a switch though. Also, preg_replace() is faster than eregi() and ereg().

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.