savagenoob Posted March 29, 2009 Share Posted March 29, 2009 I am trying to write some code to validate if form fields are filled in, but with this code, it does not echo the error message, and if filled in does not add the user... maybe one of the pros can spot my error... <?php elseif (isset($_POST['login'])) { $errors = array(); $error_msg = ''; $firstname = trim($_POST['fname']); $lastname = trim($_POST['lname']); $empid = trim($_POST['empid']); $email = trim($_POST['email']); $username = trim($_POST['uname']); $password = md5($_POST['pass']); $office = trim($_POST['office']); $agency = $_SESSION['SESS_AGENCY']; $level = trim($_POST['level']); if (empty($firstname)) { $errors[] = "First name is required."; } if (empty($lastname)) { $errors[] = "Last name is required."; } if (empty($empid)) { $errors[] = "Employee ID is required."; } if (empty($email)) { $errors[] = "Email is required."; } if (empty($username)) { $errors[] = "User name is required."; } if (empty($password)) { $errors[] = "Password is required."; } if (empty($office)) { $errors[] = "Office is required."; } if (empty($level)) { $errors[] = "User level is required."; } //Create error message if needed if (count($errors)>0) { $error_msg = "The following errors occured. Please correct and resubmit the form:<br />\n"; $error_msg .= "<ul><li>" . implode("</li>\n<li>", $errors) . "</li></ul>"; echo $error_msg; } else { $usql = mysql_query("INSERT INTO members SET firstname = '$firstname', lastname = '$lastname', empid = '$empid', login = '$username', passwd = '$password', office = '$office', agency = '$agency', email = '$email', level = '$level'"); echo mysql_error(); mysql_free_result($usql); echo "<b>User Added Successfully!<br>"; echo "<meta http-equiv=Refresh content=2;url=admin.php>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151653-solved-form-validation-problem/ Share on other sites More sharing options...
pcw Posted March 29, 2009 Share Posted March 29, 2009 Try this to carry out the validation part if ( $firstname == '') { $errors[] = "First name is required."; } elseif ($lastname == '') { $errors[] = "Last name is required."; } elseif ($empid == '') { $errors[] = "Employee ID is required."; } elseif ($username == '') { $errors[] = "Username is required."; } elseif ($password == '') { $errors[] = "Password is required."; } elseif ($office == '') { $errors[] = "Office is required."; } elseif ($level == '') { $errors[] = "User level is required."; } Quote Link to comment https://forums.phpfreaks.com/topic/151653-solved-form-validation-problem/#findComment-796418 Share on other sites More sharing options...
savagenoob Posted March 29, 2009 Author Share Posted March 29, 2009 From what I can tell that does the same thing as the empty() function I am using... tell me if I'm wrong. Quote Link to comment https://forums.phpfreaks.com/topic/151653-solved-form-validation-problem/#findComment-796452 Share on other sites More sharing options...
savagenoob Posted March 30, 2009 Author Share Posted March 30, 2009 Ima noob, I was messing with this and changed the form names... changed them back and it works fine. Quote Link to comment https://forums.phpfreaks.com/topic/151653-solved-form-validation-problem/#findComment-796611 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.