blt4424 Posted October 10, 2010 Share Posted October 10, 2010 I'm trying to do a basic form with php which asks for first, last name, and age with validation. I'm having trouble with my validation and the error counter. I want it to redisplay the form with the errors and entered input. My formhandler.php is here: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Form Handler</title></head><body><?php$fName = validateInput($_POST['fName']);$lName = validateInput($_POST['lName']);$age = validateAge($_POST['age']); function displayRequired($fieldName){echo "The field \"$fieldName\" is required.<br />n";}function validateInput($fName, $lName){if ($errorCountInput>0){echo "Please re-enter the information below.<br />\n";redisplayForm($firstName, $lastName);}else{if(empty($fName, $lName)){ displayRequired($fieldName); ++$errorCount; $returnval = "";}else{ $returnval = trim($fName,$lName); $returnval = stripslashes($returnval);}return($returnval);}}$erorrCount= 0;function validateAge($age){global $errorCount;if ($errorCount>0){echo "Please re-enter the information below.<br />\n";redisplayForm($age); }else{if(empty($age)){ echo "<p>The age field is required!</p>\n"; ++$errorCount; $returnval = ""; redisplayForm($fName, $lName, $age); }else{ $age = trim($age); $age = stripslashes($age); if(is_numeric($age)){ $age = round($age); if(($age >= 1) && ($age <= 130)){ $agereturnval = $age; } else { echo "<p>The age field must be between 1 and 130.</p>\n"; ++$errorCount; $agereturnval = ""; } }else { echo "<p>The age field must be a number between 1 and 130.</p>\n"; ++$errorCount; $returnval = ""; }}return($returnval); }$errorcount = 0;echo "Hi ".$fName." ".$lName."."."<br />";echo "You are ".$age." years old. <br />";echo $_SERVER['SCRIPT_FILENAME'];?></body></html> Any help would be much appreciated. I've looked at this for hours and I just need another set of eyes to pick some things out. Once you get too close to code it gets hard to figure it out. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 10, 2010 Share Posted October 10, 2010 $errorCount++; Quote Link to comment Share on other sites More sharing options...
blt4424 Posted October 10, 2010 Author Share Posted October 10, 2010 Ok didn't know that. I'm having trouble on line 28 in validateInput, something with my if statement? function validateInput($fName, $lName){if ($errorCountInput>0){echo "Please re-enter the information below.<br />\n";redisplayForm($firstName, $lastName);}else{[b]if(empty($fName, $lName)){[/b] displayRequired($fieldName); $errorCount++; $returnval = "";}else{ $returnval = trim($fName,$lName); $returnval = stripslashes($returnval);}return($returnval);}}$erorrCount= 0; Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 10, 2010 Share Posted October 10, 2010 what is the value of $errorCountInput? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 10, 2010 Share Posted October 10, 2010 if( empty($first_var) || empty($second_var) ) Quote Link to comment Share on other sites More sharing options...
blt4424 Posted October 11, 2010 Author Share Posted October 11, 2010 Whoops that's not supposed to say errorCountInput Thanks Pikachu! Now... I'm getting an issue with the last line, </html>...? I think that means I have a missing curly brace, right? Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted October 11, 2010 Share Posted October 11, 2010 Quick count suggests that function validateAge($age){ doesn't have a closing brace. Quote Link to comment Share on other sites More sharing options...
blt4424 Posted October 11, 2010 Author Share Posted October 11, 2010 Thanks I counted like 6 times and couldn't keep track... Now it runs but it doesn't echo anything I input. This validation is kicking my butt. Too many if statements together... Is it ok how I have errorCount for both inputValidation and ageValidation? I was unsure how I could do validation function for both all in one. Might've made this simpler... However, he's my output as of now... eek! I really appreciate your help guys. I'm a newbie to this! Warning: Missing argument 2 for validateInput(), called in C:\wamp\www\formhandler.php on line 13 and defined in C:\wamp\www\formhandler.php on line 22 Notice: Undefined variable: errorCount in C:\wamp\www\formhandler.php on line 23 Notice: Undefined variable: fieldName in C:\wamp\www\formhandler.php on line 29 The field "" is required. n Notice: Undefined variable: errorCount in C:\wamp\www\formhandler.php on line 30 Warning: Missing argument 2 for validateInput(), called in C:\wamp\www\formhandler.php on line 14 and defined in C:\wamp\www\formhandler.php on line 22 Notice: Undefined variable: errorCount in C:\wamp\www\formhandler.php on line 23 Notice: Undefined variable: fieldName in C:\wamp\www\formhandler.php on line 29 The field "" is required. n Notice: Undefined variable: errorCount in C:\wamp\www\formhandler.php on line 30 Notice: Undefined variable: returnval in C:\wamp\www\formhandler.php on line 76 Hi . You are years old. C:/wamp/www/formhandler.php Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 11, 2010 Share Posted October 11, 2010 read the warnings. Quote Link to comment Share on other sites More sharing options...
pengu Posted October 11, 2010 Share Posted October 11, 2010 Output all your variables, echo them. Make sure they are all being populated. Quote Link to comment Share on other sites More sharing options...
blt4424 Posted October 12, 2010 Author Share Posted October 12, 2010 Ok I tried combining the $fName and $lName, however, I'm having trouble with those if(!validateInput ()) statements. I'm getting a parse error. Commenting those out and trying to run my code, it somewhat works, displaying the input except for the age input. However it will give me the erorr saying it wasn't between 1 and 130 when the input was say "20". Also, if I don't enter say a first name, it gives me the error to re-enter the info, but doesn't redisplay the form with the input. Here's my code at the moment. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Form Handler</title> </head> <body> <?php $fName = validateInput($_POST['fName']); $lName = validateInput($_POST['lName']); $age = validateAge($_POST['age']); function displayRequired($fieldName){ echo "The field \"$fieldName\" is required.<br />n"; } if (!validateInput($_POST['fName']) { echo "First name is required.<br />n"; } if (!validateInput($_POST['lName']) { echo "Last name is required.<br />n"; } function validateInput($inputString){ if (empty($inputString)){ echo "Please re-enter the information below.<br />\n"; redisplayForm($inputString); } else{ if(empty($inputString)){ displayRequired($fieldName); $errorCount++; $returnval = ""; } else{ $returnval = trim($inputString); $returnval = stripslashes($returnval); } return($returnval); } if($errorCount==0){ return true; }else { return false; } } $erorrCount= 0; function validateAge($age){ global $errorCount; if ($errorCount>0){ echo "Please re-enter the information below.<br />\n"; redisplayForm($$age); } else{ if(empty($age)){ echo "<p>The age field is required!</p>\n"; $errorCount++; $returnval = ""; redisplayForm($age); } else{ $age = trim($age); $agereturnval = ""; $age = stripslashes($agereturnval); if(is_numeric($age)){ $age = round($age); if(($age >= 1) && ($age <= 130)){ $agereturnval = $age; } else { echo "<p>The age field must be between 1 and 130.</p>\n"; $errorCount++; $agereturnval = ""; } }else { echo "<p>The age field must be a number between 1 and 130.</p>\n"; $errorCount++; $agereturnval = ""; } } return($agereturnval); } } $errorcount = 0; echo "Hi ".$fName." ".$lName."."."<br />"; echo "You are ".$age." years old. <br />"; echo $_SERVER['SCRIPT_FILENAME']; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
blt4424 Posted October 12, 2010 Author Share Posted October 12, 2010 Stupid me, I didn't have a redisplayForm function with the form code. However, it isn't redisplaying the input. <?php $fName = validateInput($_POST['fName']); $lName = validateInput($_POST['lName']); $age = validateAge($_POST['age']); function redisplayForm($inputString, $age){ ?> <form action = "formhandler.php" method = "post"> First name: <input type = "text" name = "fName" /> <br /> Last name: <input type = "text" name = "lName" /> <br /> Age: <input type = "text" name = "age" /> <br /> <input type = "submit" value = "SUBMIT" /> </form> <?php } function displayRequired($fieldName){ echo "The field \"$fieldName\" is required.<br />n"; } function validateInput($inputString){ if (empty($inputString)){ echo "Please re-enter the information below.<br />\n"; redisplayForm($inputString, $age); } else{ if(empty($inputString)){ displayRequired($fieldName); $errorCount++; $returnval = ""; } else{ $returnval = trim($inputString); $returnval = stripslashes($returnval); } return($returnval); } if($errorCount==0){ return true; }else { return false; } } $erorrCount= 0; function validateAge($age){ global $errorCount; if ($errorCount>0){ echo "Please re-enter the information below.<br />\n"; redisplayForm($inputString, $age); } else{ if(empty($age)){ echo "<p>The age field is required!</p>\n"; $errorCount++; $returnval = ""; redisplayForm($inputString, $age); } else{ $age = trim($age); $agereturnval = ""; $age = stripslashes($agereturnval); if(is_numeric($age)){ $age = round($age); if(($age >= 1) && ($age <= 130)){ $agereturnval = $age; } else { echo "<p>The age field must be between 1 and 130.</p>\n"; $errorCount++; $agereturnval = ""; } }else { echo "<p>The age field must be a number between 1 and 130.</p>\n"; $errorCount++; $agereturnval = ""; } } return($agereturnval); } } $errorcount = 0; echo "Hi ".$fName." ".$lName."."."<br />"; echo "You are ".$age." years old. <br />"; echo $_SERVER['SCRIPT_FILENAME']; ?> Please re-enter the information below. Notice: Undefined variable: age in C:\wamp\www\formhandler.php on line 40 First name: Last name: Age: Notice: Undefined variable: errorCount in C:\wamp\www\formhandler.php on line 54 The age field must be a number between 1 and 130. Hi bob 1. You are years old. C:/wamp/www/formhandler.php Quote Link to comment Share on other sites More sharing options...
blt4424 Posted October 12, 2010 Author Share Posted October 12, 2010 I've made alterations, however I'm now just getting a blank page... <?phpfunction redisplayForm($label='',$age=''){ if($label != '') { echo '<p>Your '.$label.'</p>'; } ?> <form action = "formhandler.php" method = "post">First name: <input type = "text" name = "fName" /> <br />Last name: <input type = "text" name = "lName" /> <br />Age: <input type = "text" name = "age" /> <br /><input type = "submit" value = "SUBMIT" /></form> <?php}function displayRequired($fieldName){echo "The field \"$fieldName\" is required.<br />n";}function validateInput($inputString, $label){if (empty($inputString)) { echo "Please re-enter the information below.<br />\n";redisplayForm($label, $age); exit(); } else { if(empty($inputString)) { $errorCount++;$returnval = ""; displayRequired($fieldName); }else {$returnval = trim($inputString);$returnval = stripslashes($returnval);}return($returnval);}if($errorCount==0) { return true; } else { return false; }}function validateAge($age,$errorCount) {if ($errorCount>0) {echo "Please re-enter the information below.<br />\n";redisplayForm($inputString, $age); exit();}else {if(empty($age)) { echo "<p>The age field is required!</p>\n"; $errorCount++; $returnval = ""; redisplayForm($inputString, $age); exit(); } else { $age = intval(trim($age)); if(is_numeric($age)) { $age = round($age); if(($age >= 1) && ($age <= 130)) { $agereturnval = $age; } else { echo "<p>The age field must be between 1 and 130.</p>\n"; ++$errorCount; $agereturnval = ""; } } else { echo "<p>The age field must be a number between 1 and 130.</p>\n"; ++$errorCount; $agereturnval = ""; }}return($agereturnval);} return $age;}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Form Handler</title></head><body><?phpif(isset($_POST['submit'])) { $errorCount=0; $fName = validateInput($_POST['fName'], "First name"); $lName = validateInput($_POST['lName'], "Last name"); $age = validateAge($_POST['age'], "Age"); if($errorCount ==0) {echo "Hi ".$fName." ".$lName."."."<br />";echo "You are ".$age." years old. <br />";echo $_SERVER['SCRIPT_FILENAME'];}else{ redisplayForm();} }?></body></html> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 12, 2010 Share Posted October 12, 2010 May I ask why you're trying to do all of this with these functions? It all seems a bit unnecessary and overcomplicated, especially since you have more functions than you do form fields. Quote Link to comment Share on other sites More sharing options...
blt4424 Posted October 12, 2010 Author Share Posted October 12, 2010 Perhaps you're right, but this is how I've learned <?phpfunction redisplayForm($label='',$age=''){ if($label != '') { echo '<p>Your '.$label.' is required!'.'</p>'; } ?><form action = "formhandler.php" method = "post">First name: <input type = "text" name = "fName" /> <br />Last name: <input type = "text" name = "lName" /> <br />Age: <input type = "text" name = "age" /> <br /><input type="submit" name="submit" value = "SUBMIT" /></form><?php}function displayRequired($fieldName){ echo "The field \"$fieldName\" is required.<br />n";}function validateInput($inputString,$label){ if (empty($inputString)) { echo "Please re-enter the information below.<br />\n"; redisplayForm($label, $age); exit(); } else { if(empty($inputString)) { $errorCount++; $returnval = ""; displayRequired($fieldName); } else { $returnval = trim($inputString); $returnval = stripslashes($returnval); } return($returnval); } if($errorCount==0) { return true; } else { return false; }}function validateAge($age,$errorCount){ if ($errorCount > 0) { echo "Please re-enter the information below.<br />\n"; redisplayForm($label, $age); exit(); } else { if(empty($age)) { echo "<p>The age field is required!</p>\n"; $errorCount++; $returnval = ""; redisplayForm($inputString, $age); exit(); } else { $age = intval(trim($age)); if(is_numeric($age)) { $age = round($age); if(($age >= 1) && ($age <= 130)) { $agereturnval = $age; } else { echo "<p>The age field must be between 1 and 130.</p>\n"; $errorCount++; $agereturnval = ""; } } else { echo "<p>The age field must be a number between 1 and 130.</p>\n"; $errorCount++; $agereturnval = ""; } } return($agereturnval); } return $age;}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body><?phpif(isset($_POST['submit'])){ $errorCount= 0; $fName = validateInput($_POST['fName'],'First Name'); $lName = validateInput($_POST['lName'],'Last Name'); $age = validateAge($_POST['age'],$errorCount); if($errorCount == 0) { echo "Hi ".$fName." ".$lName."."."<br />"; echo "You are ".$age." years old. <br />"; echo $_SERVER['SCRIPT_FILENAME']; }}else{ redisplayForm();}?></body></html> Works but get Notice: Undefined variable: age in C:\wamp\www\formhandler.php on line 26 Which is...(in bold) function validateInput($inputString,$label) { if (empty($inputString)) { echo "Please re-enter the information below.<br />\n"; redisplayForm($label, $age); exit(); Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 12, 2010 Share Posted October 12, 2010 This is a quick rework of that form, without the functions. It will now display any fields that were previously entered, and any errors encountered. If the form is submitted error-free the welcome message is displayed without the form. It's also about 60 lines shorter. The point is there is no need to write a function for everything in the script. If I was going to write any function for that script, it would be only to validate the name fields since they have basically the same requirements. That would save another 10 or so lines of code, but I really didn't think it was worth the trouble. If I knew I could use the function in other places in the project, I'd put it in an external file and include() it where needed. <?php if( $_POST['submitted'] == 'yes' ) { //check value of new hidden field to verify form submission array_map('trim', $_POST); // trim() then entire $_POST array at once $errors = array(); // initialize an array to store validation errors if( !empty($_POST['fName']) ) { //validate first name to have a value and that the value is alpha if( ctype_alpha($_POST['fName']) ) { $fName = $_POST['fName']; } else { $errors[] = 'First name contains invalid characters.'; } } else { $errors[] = 'First name is a required field.'; } if( !empty($_POST['lName']) ) { // validate the last name to have a value and that the value is alpha if( ctype_alpha($_POST['lName']) ) { $lName = $_POST['lName']; } else { $errors[] = 'Last name contains invalid characters.'; } } else { $errors[] = 'Last name is a required field.'; } if( !empty($_POST['age']) ) { // validate age to have a value and the value is within the range of 10- 105 $range = range(10, 105); if( ctype_digit($_POST['age']) || (!in_array($_POST, $range)) ) { $age = (int) $_POST['age']; } else { $errors[] = 'Age must be a number between 10 - 105'; } } else { $errors[] = 'Age is a required field.'; } if( !empty($errors) ) { // if there are validation errors, display them $n = count($errors); $i = 1; foreach( $errors as $v ) { echo "<font color=\"red\">$v</font>"; echo $i < $n ? '<br />' : ''; $i++; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Form Handler</title> </head> <body> <?php if( ($_POST['submitted'] == 'yes' && !empty($errors)) || $_POST['submitted'] != 'yes' ) { // if the form has not been submitted, or there are errors, display the form along with any values entered ?> <form action = "formhandler.php" method = "post"> First name: <input type = "text" name = "fName" value="<?php echo !empty($_POST['fName']) ? $_POST['fName'] : ''; ?>" /> <br /> Last name: <input type = "text" name = "lName" value="<?php echo !empty($_POST['lName']) ? $_POST['lName'] : ''; ?>" /> <br /> Age: <input type = "text" name = "age" value="<?php echo !empty($_POST['age']) ? $_POST['age'] : ''; ?>"/> <br /> <input type="hidden" name="submitted" value="yes" /> <!-- hidden field to pander to some browsers' improper handling of submit buttons --> <input type = "submit" value = "SUBMIT" /> </form> <?php } else { // if form submitted and free of errors, display message. echo "Welcome $fName $lName. You are $age years old."; } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
blt4424 Posted October 12, 2010 Author Share Posted October 12, 2010 OK, one last question. I have a php program that has the form, the form then goes to the form handler for validation,printing. However, I have to hit the submit button twice for it to do anything. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 12, 2010 Share Posted October 12, 2010 I'd need to see all of the code to get a decent idea of what's going on with it. Could be any number of things. Quote Link to comment Share on other sites More sharing options...
blt4424 Posted October 12, 2010 Author Share Posted October 12, 2010 I change post to get, thinking that might be the problem, but no. <?php function redisplayForm($label='', $age='') { if($label != '') { echo '<p>Your '.$label.' is required!'.'</p>'; } ?> <form action = "formhandler.php" method = "get"> First name: <input type = "text" name = "fName" value ="<?php echo $_GET['fName'];?>" /> <br /> Last name: <input type = "text" name = "lName" value ="<?php echo $_GET['lName'];?>" /> <br /> Age: <input type = "text" name = "age" value ="<?php echo $_GET['age'];?>"/> <br /> <input type="submit" name="submit" value = "SUBMIT" /> </form> <?php } function displayRequired($fieldName) { echo "The field \"$fieldName\" is required.<br />n"; } function validateInput($inputString,$label) { if (empty($inputString)) { echo "Please re-enter the information below.<br />\n"; $errorCount++; redisplayForm($label); exit(); } else { if(empty($inputString)) { $errorCount++; $returnval = ""; displayRequired($fieldName); } else { $returnval = trim($inputString); $returnval = stripslashes($returnval); } return($returnval); } if($errorCount==0) { return true; } else { return false; } } function validateAge($age,$errorCount) { if ($errorCount > 0) { echo "Please re-enter the information below.<br />\n"; redisplayForm($age); exit(); } else { if(empty($age)) { echo "<p>The age field is required!</p>\n"; $errorCount++; $returnval = ""; redisplayForm($age); exit(); } else { $age = intval(trim($age)); if(is_numeric($age)) { $age = round($age); if(($age >= 1) && ($age <= 130)) { $agereturnval = $age; } else { echo "<p>The age field must be between 1 and 130.</p>\n"; redisplayForm($age); $errorCount++; $agereturnval = ""; } } else { echo "<p>The age field must be a number between 1 and 130.</p>\n"; redisplayForm($age); $errorCount++; $agereturnval = ""; } } return($agereturnval); } return $age; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php if(isset($_GET['submit'])) { $errorCount= 0; $fName = validateInput($_GET['fName'],'First Name'); $lName = validateInput($_GET['lName'],'Last Name'); $age = validateAge($_GET['age'],'Age'); if($errorCount == 0) { echo "Hi ".$fName." ".$lName."."."<br />"; echo "You are ".$age." years old. <br />"; echo $_SERVER['SCRIPT_FILENAME']; } } else { redisplayForm(); } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 12, 2010 Share Posted October 12, 2010 Works fine for me with both Firefox and Safari . . . Quote Link to comment 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.