PC Nerd Posted January 4, 2008 Share Posted January 4, 2008 Hi, This is my first major validation - mainly because I've started to user preg_match etc etc. Ive got most of the script working - and I know that it is catchign the errors. The problem is that I can't seem to get the errors into my $ERROR array... My script is below - and is also attached. The script stopped entering the data when i added the two functions called "issErr()" and "setError()". That part of the code is meant to stop having duplicate errors on the same field. I appreciate any help. I know that most people dont like to go through large scripts for free - but I would appreciate any pointers that may help to solve my problem. Thanks for any and all help. NOTE: there are some echo statements hat seem irelevent. I added them to find out where my script was stopping etc - so just to let you know. <?php global $ERROR; $ERROR = array(); function isErr($fieldname) { if(!isset($ERROR)) { $ERROR = array(0 => "meh"); } foreach($ERROR as $field => $value) { if($field['empty'] == $fieldname) { return 0;} } return 1; } function setError($field, $type) { echo "Setting Error<br>"; $ERROR[$idx] = array("err" => $type, "empty" => $field); #$ERROR[$idx]['err'] = $type; #$ERROR[$idx]['empty'] = $field; $idx ++; } #$ERROR[iD]['err'] = "TYPE"; #$ERROR[iD]['empty'] = "FIELD_IDENTIFIER"; #$idx++; # Increment the Error ID $idx = 0; if(isset($_GET['action']) && $_GET['action'] = "validate") { if($_POST['Save'] == 1 || $_POST['Save'] == 0) { pass; } else { if(isErr("Save") == 1){ setError("Save", "empty"); } } echo "Stage 1<br><br>"; foreach($_POST as $field => $value) { echo "Stage 1.1 <br>"; if($field == 1) {continue;} if($_POST['Save'] == 1 && $field == "User_Name" || $field == "Password" || $field == "Conf_Password" || $field == "Emg_Relation") { ### Validate the Username, password and email if(empty($value)) { if(isErr($field) == 1){ setError($field, "empty"); } } else if(!preg_match("[A-za'-z]", $value)) { if(isErr($field) == 1){ setError($field, "invalid"); } } else if(strlen($value) > 20 || strlen($value) < 5) { if(isErr($field) == 1){ setError($field, "invalid"); } } else if($field == "Password" && $value != $_POST['Conf_Password']) { if(isErr($field) == 1){ setError($field, "passmtch"); } } } if($_POST['Save'] == 0 && empty($value)) { if(isErr($field) == 1){ setError($field, "empty"); } } if($field == "F_Name" || $field == "L_Name" || $field == "User_Name" || $field == "Emg_Name") { ### Validate the names if(!ereg("[A-za-z' -]", $value)) { if(isErr($field) == 1){ setError($field, "invalid"); } } else if(strlen($value) > 20) { if(isErr($field) == 1){ setError($field, "len"); } } } if($field == "Email") { if (!preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $input_email)) { if(isErr($field) == 1){ setError($field, "invalid"); } } } if($field == "Carrier" && $value = "---") { if(isErr($field) == 1){ setError($field, "empty"); } } if($field == "DOB") { if(strlen($value) > 10 || preg_match('/^(0[1-9]|1[012])[\\/-](0[1-9]|[12][0-9]|3[01])[\\/-](19[0-9]{2}|2[0-9]{3})$/', $input_date)) { if(isErr($field) == 1){ setError($field, "invalid"); } } } if($field == "Address") { $value = trim($value); if(strlen($value) > 50 || preg_match("^[a-zA-Z\d]+(([\'\,\.\- #][a-zA-Z\d ])?[a-zA-Z\d]*[\.]*)*$^", $value)) { if(isErr($field) == 1){ setError($field, "invalid"); } } } if($field == "Cell" || $field = "Emg_Phone" || !preg_match("^(\d{3}-\d{3}-\d{4})*$", $value || strlen($value) > 10)) { ### Validate phone numbers if(isErr($field) == 1){ setError($field, "invalid"); } } } echo "Stage 2<br><br>"; if(isset($ERROR)) { echo "Stage 3<br><br"; ### If errors then display messages $FIELDS['F_Name'] = "First Name"; $FIELDS['L_Name'] = "Last Name"; $FIELDS['Email'] = "Email"; $FIELDS['User_Name'] = "User Name"; $FIELDS['Password'] = "Password"; $FIELDS['Conf_Password'] = "Confirm Password"; $FIELDS['Cell_no'] = "Cell Number"; $FIELDS['Carrier'] = "Carrier"; $FIELDS['DOB'] = "Date of Birth"; $FIELDS['Address'] = "Address"; $FIELDS['Emg_Name'] = "Emergency Contact Name"; $FIELDS['Emg_Phone'] = "Emergency Contact Phone"; $FIELDS['Emg_Relation'] = "Emergency Contact Relation"; $FIELDS['Save'] = "Save as Profile Account"; $FRM = $_POST; echo "<pre>"; print_r($ERROR); echo "</pre>"; foreach($ERROR as $fieldinner => $inner) { echo "<p>\n"; $fieldname = $FIELDS[$inner['empty']]; if($inner['err'] == "empty") { if($fieldname == "Save") { echo "You must select if you want a saved profile account."; pass; } echo "The field ".$fieldname." has been left empty."; $FRM[$fieldinner] = ""; } else if($inner['err'] == "invalid") { echo "The field ".$fieldname." is invalid. Please re-enter."; $FRM[$fieldinner] = ""; } else if($inner['err'] == "passmtch") { echo "Password do not match"; $FRM[$fieldinner] = ""; } else if($inner['err'] == "len") { echo "The field ".$fieldname." is too long."; $FRM[$fielinner] = ""; } echo "<p>\n"; } echo "Stage 4<br><br>"; } else { ### Submit to database } } $FIELDS['F_Name'] = "First Name"; $FIELDS['L_Name'] = "Last Name"; $FIELDS['Email'] = "Email"; $FIELDS['User_Name'] = "User Name"; $FIELDS['Password'] = "Password"; $FIELDS['Conf_Password'] = "Confirm Password"; $FIELDS['Cell_no'] = "Cell Number"; $FIELDS['Carrier'] = "Carrier"; $FIELDS['DOB'] = "Date of Birth"; $FIELDS['Address'] = "Address"; $FIELDS['Emg_Name'] = "Emergence Contact Name"; $FIELDS['Emg_Phone'] = "Emergency Contact Phone"; $FIELDS['Emg_Relation'] = "Emergency Contact Relation"; $FIELDS['Save'] = "Save as Profile Account"; ?> <?php ### DISPLAY THE FORM echo "<form action = 'new_user.php?action=validate' method = 'POST'>\n"; echo "<h3>About You</h3>\n"; echo "<p>First Name: <input type = 'text' name = 'F_Name' value = '".$FRM['F_Name']."' />*</p>\n"; echo "<p>Last Name: <input type = 'text' name = 'L_Name' value = '".$FRM['L_Name']."' />*</p>\n"; echo "<p>Email: <input type = 'text' name = 'Email' value = '".$FRM['Email']."' />*</p>\n"; echo "<be>\n"; echo "<p>User Name: <input type = 'text' name = 'User_Name' value = '".$FRM['User_Name']."' />^</p>\n"; echo "<p>Password: <input type = 'pass' name = 'Password' value = '".$FRM['Password']."' />^</p>\n"; echo "<p>Confirm Password: <input type = 'pass' name = 'Conf_Password' value = '".$FRM['Conf_Password']."' />^</p>\n"; echo "<br />\n"; echo "<p>Cell Number: <input type = 'text' name = 'Cell_no' value = '".$FRM['Cell_no']."' />*</p>\n"; echo "<p>Cell Carrier: <select name = 'Carrier'>\n"; echo " <option selected>---</option>\n"; echo " <option value='att'>AT&T\Cingular</option>\n"; echo " <option value='sprint'>Sprint</option>\n"; echo " <option value='verizon'>Verizon</option>\n"; echo " <option value='tmob'>T-Mobile</option>\n"; echo " <option value='virgmob'>Virgin Mobile</option>\n"; echo " <option value='nextel'>Nextel</option>\n"; echo " <option value='Other'>Other</option>\n"; echo " </select>*\n"; echo "</p>\n"; echo "<p>DOB: <input type = 'text' name = 'DOB' value = '".$FRM['DOB']."' />*</p>\n"; echo "<p>Address: <input type = 'text' name = 'Address' value = '".$FRM['Address']."' />*</p>\n"; echo "<br>\n"; echo "<h3>Emergency Contact</h3>\n"; echo "<p>Contact Name: <input type = 'text' name = 'Emg_Name' value = '".$FRM['Emg_Name']."' />*</p>\n"; echo "<p>Contact Phone Number: <input type = 'text' name = 'Emg_Phone' value = '".$FRM['Emg_Phone']."' />*</p>\n"; echo "<p>Contact Relation: <input type = 'text' name = 'Emg_Relation' value = '".$FRM['Rmg_Relation']."' />*</p>\n"; echo "<br />\n"; echo "<p>Save your details as a profile account? : <input type = 'radio' name = 'Save' value = '1'>Yes</input> <input type = 'radio' name = 'Save' value = '0'>No</input> *</p>\n"; echo "<input type = 'submit' value = 'Submit'></input> <input type = 'reset' value = 'reset'></input>\n"; echo "</form>\n"; echo "<br>\n"; echo "<p>* Required Fields.<br>^ Requried for saved profile user - in addition to other fields.</p>\n"; ?> Thanks [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/84436-form-validation-not-working/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 4, 2008 Share Posted January 4, 2008 The "global" keyword is used inside of functions, not in the main program. It makes a main program variable available inside of a function. It needs to be used in each function that references the $ERROR array. Quote Link to comment https://forums.phpfreaks.com/topic/84436-form-validation-not-working/#findComment-430093 Share on other sites More sharing options...
PC Nerd Posted January 4, 2008 Author Share Posted January 4, 2008 function isErr($fieldname) { if(!isset($ERROR)) { global $ERROR; $ERROR = array(); } foreach($ERROR as $field => $value) { if($field['empty'] == $fieldname) { return 0;} } return 1; } is my new function. is that correct? Thansk for the fast reply Quote Link to comment https://forums.phpfreaks.com/topic/84436-form-validation-not-working/#findComment-430094 Share on other sites More sharing options...
redarrow Posted January 4, 2008 Share Posted January 4, 2008 function isErr($fieldname) { $ERROR = array(); << here i say if(!isset($ERROR)) { } foreach($ERROR as $field => $value) { if($field['empty'] == $fieldname) { return 0;} } return 1; } Quote Link to comment https://forums.phpfreaks.com/topic/84436-form-validation-not-working/#findComment-430097 Share on other sites More sharing options...
PFMaBiSmAd Posted January 4, 2008 Share Posted January 4, 2008 You need to define the $ERROR variable as an array in your main code. In the function you must use global $ERROR; before any reference is made to it in the code. Edit: Reading the examples at this link will probably help - http://www.php.net/manual/en/language.variables.scope.php Quote Link to comment https://forums.phpfreaks.com/topic/84436-form-validation-not-working/#findComment-430099 Share on other sites More sharing options...
PC Nerd Posted January 4, 2008 Author Share Posted January 4, 2008 $ERROR = array(); function isErr($fieldname, $ERROR) { foreach($ERROR as $field => $value) { if($field['empty'] == $fieldname) { return 0;} } return 1; } if(isErr($field, $ERROR) == 1){ setError($field, "empty"); } Ive changed the function to accept the $ERROR argument to edit - however do i need to return ERROR - and in which case how would i store it.. etc? thanks Quote Link to comment https://forums.phpfreaks.com/topic/84436-form-validation-not-working/#findComment-430105 Share on other sites More sharing options...
PC Nerd Posted January 5, 2008 Author Share Posted January 5, 2008 **bump** Quote Link to comment https://forums.phpfreaks.com/topic/84436-form-validation-not-working/#findComment-430942 Share on other sites More sharing options...
PC Nerd Posted January 5, 2008 Author Share Posted January 5, 2008 **bump** any suggestions ? Thanks for all help so far Quote Link to comment https://forums.phpfreaks.com/topic/84436-form-validation-not-working/#findComment-431075 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.