richiejones24 Posted December 8, 2011 Share Posted December 8, 2011 I am trying to design a form validation script, using an array to display errors,but its not working and i am not sure where I am going wrong. <? $confcode = rand(); $pageid = "reg6"; $root = $_SERVER['DOCUMENT_ROOT']; require("$root/include/incpost.php"); require("$root/include/mysqldb.php"); if ($_POST['firstname'] == NULL) { $error[] = "First Name Field Is Empty"; } if ($_POST['lastname'] == NULL) { $error[] = "Last Name Field Is Empty"; } if ($_POST['phonea'] == NULL) { $error[] = "Phone Number Field Is Empty"; } elseif (!is_numeric($_POST['phonea'])) { $error[] = "Phone Number Can Only Contain Numbers"; } if ($_POST['username'] == NULL) { $error[] = "User Name Field Is Empty"; } elseif (strlen($_POST['username']) <= 4) { $error[] = "Username Is Too Short"; } if($_POST['email1'] !== $_POST['email2']) {$error[] = "Email's Do Not Match"; } elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email1'])) {$error[] = "Email Address Is Not Valid"; } if (strlen($_POST['password1']) <= 6) { $error[] = "Password Is To Short"; } elseif ($password1 !== $password2) { $error[] = "Passwords Do Not Mach"; } if (preg_match('/[^a*()-z0@£"%&-9.#$-]/i', $_POST['password1'])) { $error[] = "Password Contains Invalid Charactors"; } if (preg_match('/[^a*()-z0@£"%&-9.#$-]/i', $_POST['username'])) { $error[] = "Username Contains Invalid Charactors"; } if ([array count] == 0) //if no errors add to DB { mysql_query("INSERT INTO Reg_Profile_Private (UIN, firstname, lastname, email, phone, password, username) VALUES ('$uin', '$firstname', '$lastname', '$email', '$phone', '$passwordenc', '$username')"); } else { //else print errors $error[] = array(); $active_keys = array(); foreach($error as $key => $myvar) {print "$myvar </p>\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252786-form-validation-array-errors/ Share on other sites More sharing options...
ohdang888 Posted December 8, 2011 Share Posted December 8, 2011 try <?php if (!isset($error)) //if no errors add to DB { mysql_query("INSERT INTO Reg_Profile_Private (UIN, firstname, lastname, email, phone, password, username) VALUES ('$uin', '$firstname', '$lastname', '$email', '$phone', '$passwordenc', '$username')"); } else { //else print errors foreach($error as $key => $myvar) {print "$myvar </p>\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252786-form-validation-array-errors/#findComment-1295986 Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 Firstly, you should use isset() instead of == null. In your code, if a field is left out you will get a PHP Notice: undefined index. Secondly I believe this is your problem: if ([array count] == 0) It should be if (empty($error)) { Quote Link to comment https://forums.phpfreaks.com/topic/252786-form-validation-array-errors/#findComment-1295987 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.