tqla Posted April 2, 2008 Share Posted April 2, 2008 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 { ?> Quote Link to comment https://forums.phpfreaks.com/topic/99221-solved-help-using-more-descriptive-names-in-error-message/ Share on other sites More sharing options...
Daniel0 Posted April 2, 2008 Share Posted April 2, 2008 You could change if ($field != "fax") { if ($value == "") { $blanks[] = $field; } } to if ($field != "fax") { if ($value == "") { $blanks[] = $labels[$field]; } } Quote Link to comment https://forums.phpfreaks.com/topic/99221-solved-help-using-more-descriptive-names-in-error-message/#findComment-507707 Share on other sites More sharing options...
tqla Posted April 2, 2008 Author Share Posted April 2, 2008 Thanks Daniel0. That works perfectly. Another problem exists. For some reason the Validate Data section is not working. What am I missing? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/99221-solved-help-using-more-descriptive-names-in-error-message/#findComment-507770 Share on other sites More sharing options...
Daniel0 Posted April 2, 2008 Share Posted April 2, 2008 How doesn't it work? Does it give unexpected results/output or does the foreach never execute? Quote Link to comment https://forums.phpfreaks.com/topic/99221-solved-help-using-more-descriptive-names-in-error-message/#findComment-507838 Share on other sites More sharing options...
tqla Posted April 2, 2008 Author Share Posted April 2, 2008 It never executes. No data is validated. Quote Link to comment https://forums.phpfreaks.com/topic/99221-solved-help-using-more-descriptive-names-in-error-message/#findComment-507842 Share on other sites More sharing options...
Daniel0 Posted April 2, 2008 Share Posted April 2, 2008 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(). Quote Link to comment https://forums.phpfreaks.com/topic/99221-solved-help-using-more-descriptive-names-in-error-message/#findComment-507853 Share on other sites More sharing options...
tqla Posted April 2, 2008 Author Share Posted April 2, 2008 Thanks Daniel0 for the suggestions. Will do. Quote Link to comment https://forums.phpfreaks.com/topic/99221-solved-help-using-more-descriptive-names-in-error-message/#findComment-507889 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.