errcricket Posted March 31, 2009 Share Posted March 31, 2009 greetings. in dire need of constructive opinions here... i have created a form that collects data from users. like most forms, once the submit button is pushed, if there are no problems, the data goes in the database and a congrats message appears. otherwise problematic areas are highlighted and the user is prompted to try again. depending on how the user responds to one Boolean question, certain input is required while others are not. if the user responds to this question in the opposite way but they input information into non-required fields, then those fields need to be checked was well. obviously if there are errors, i would like to display any correctly entered data (sticky). with one exception (address), strings cannot contain numbers. the function to check strings should remove all characters except a(A)-z(Z) & spaces (the address can have numbers) and perform some other checks as well. if only numbers are entered, an error message should appear - otherwise they are just scrubbed out. what i have noticed is that if i enter the first name and the email and select yes for the bool, error messages properly appear telling me i need to fill in the remaining inputs. if i then enter something with a number for the address and then hit return, i correctly see the error messages telling me to fill in the other data. at that point, i can input values with numbers and it is accepted. (?!!?) if i had tried those shenanigans before i entered the address, they would not be accepted. does anyone have some ideas about what i am doing wrong? necessary code is listed below. $listNotNull = $enteredEmailAndFname == TRUE && $selected_List == 'Yes' && $_POST['nameL'] != NULL && $_POST['cityTown'] != NULL && $_POST['streetAddress'] != NULL; $listValid = $validEmailAndFname == TRUE && checkStringInput($_POST['nameL'], 50) == 0 && checkStringInput($_POST['cityTown'], 50) == 0 && checkStringInput($_POST['streetAddress'], 100) == 0; $listCheck = $listNotNull == TRUE && $listValid == TRUE; //$noListNotNull = $_POST['nameF'] != NULL && $_POST['eMail'] != NULL && $selected_List == 'No' && ($_POST['nameL'] != NULL || $_POST['streetAddress'] != NULL || $_POST['cityTown'] != NULL); //$noCheck = $noListNotNull == TRUE && $listValid == TRUE; //$noListNull = $_POST['nameF'] != NULL && $_POST['eMail'] != NULL && $selected_List == 'No' && $_POST['nameL'] == NULL && $_POST['streetAddress'] == NULL && $_POST['cityTown'] == NULL; //$noNullCheck = $noListNull == TRUE && checkStringInput($_POST['nameF'], 20) == 0 && checkEmail($_POST['eMail'], 40) == 0; /*------------------------------------- check for correctness of entries -----------------------------------------------------*/ if($listCheck != TRUE) { if(checkStringInput($_POST['nameF'], 20) != 0) messageSpecs(checkStringInput($_POST['nameF'], 20), $messageFName); else $Fname = $_POST['nameF']; if(checkEmail($_POST['eMail'], 40) != 0) //initially only email and first name need to be entered messageSpecs(checkEmail($_POST['eMail'], 40), $messageEmail); else $eMail = $_POST['eMail']; //initially only email and first name need to be entered if($selected_List == 'Yes' && ($_POST['nameL'] == NULL || checkStringInput($_POST['nameL'], 50) != 0) ) //last N messageSpecs(checkStringInput($_POST['nameL'], 50), $messageLName); else $Lname = $_POST['nameL']; if($selected_List == 'Yes' && ($_POST['cityTown'] == NULL || checkStringInput($_POST['cityTown'], 50) != 0) )//city messageSpecs(checkStringInput($_POST['cityTown'], 50), $messageCity); else $cityortown = $_POST['cityTown']; if($selected_List == 'Yes' && ($_POST['streetAddress'] == NULL || checkStringInput($_POST['streetAddress'], 100) != 0) ) //sAddress messageSpecs(checkStringInput($_POST['streetAddress'], 100), $messageAddress); else $streetAddress = $_POST['streetAddress']; } function checkStringInput(&$string, $length) { global $case, $message; $case = 0; if ($string == $_POST['streetAddress']) $string = preg_replace('/[^a-zA-Z0-9\s]/', '', $string);//remove all characters except a(A)-z(Z), 0-9, & spaces else $string = preg_replace('/[^a-zA-Z\s]/', '', $string); //remove all characters except a(A)-z(Z) & spaces if (!isset($string) || (isset($string) && empty($string)) ) $case = 1; if (strlen($string) > $length ) $case = 2; if (gettype($string) != 'string' || is_string($string) == FALSE) $case = 3; if ($case != 0) return $case; else return ($case = 0); } //end function checkStringInput function messageSpecs($number, &$messageStatement) { global $messageString; if ($number != 0) { switch($number) { case 1: echo '<p>message for case 1</p>'; $messageString = '<P style="color:red">You must enter a value.</P>'; break; case 2: echo '<p>message for case 2</p>'; $messageString = '<P style="color:red">Your have entered an incorrect number of characters.</P>'; break; case 3: echo '<p>message for case 3</p>'; $messageString = '<P style="color:red">You did not input a valid entry</P>'; break; }// end switch $messageStatement = $messageString; }//end case if else $messageStatement = ""; } //end messageSpecs function trying to check every input can become...cumbersome and as a result, the code was getting quite long. i decided to write several functions - one for e-mail, one for numbers, and one for strings. this seemed to work pretty well (still a few bugs), but i thought there must be a more efficient way to do it so i decided for all string input, i would make an array and loop through it to run the string check function. unfortunately, if i purposely input incorrect data, when i am prompted to re-enter certain data, the proper data does not stick. Link to comment https://forums.phpfreaks.com/topic/151978-string-check-function-has-undesired-order-dependant-characteristics/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.