saintryan Posted March 7, 2009 Share Posted March 7, 2009 Hello all I'm currently creating a basic form it has various fields in it. One of them is age.. What i am trying to do is when someone submits their age with the form the php scripts validates it's an integer for a start and then check to see if the age is within a certain bracket like >16 but <26. here is what i have done so far: test.html <form action="myform.php" method="post"> <p>Firstname: *** <input type="text" name="firstname" /><br /> Surname: *** <input type="text" name="surname" /> <br /> Address: <input type="text" name="addressone" /> <br /> Postcode: <input type="text" name="postcode" /><br /> E-mail: <input type="text" name="email" /></p> <p> Age: ** <input type="number" name="age" /><br /> <br /> <br /> </p> <p><input type="submit" value="Send it!"></p> </form> myform.php <?php $firstname = check_input($_POST['firstname'], "Enter you firstname please"); $surname = check_input($_POST['surname'], "Enter you surnname please"); $addressone = check_input($_POST['adressone']); $postcode = check_input($_POST['postcode']); $email = check_input($_POST['email']); $age = check_input($_POST['vage'], "Please enter your age"); function check_input($data, $error='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($error && strlen($data) == 0) { show_error($error); } else return $data; } function show_error($myError) { ?> <html> <body> <b>Please correct the following error:</b><br /> <?php echo $myError; ?> </body> </html> <?php exit(); } ?> <html> <body> Your first name is: <?php echo $firstname; ?><br /> Your Surname is: <?php echo $surname; ?><br /> Your address is: <?php echo $addressone; ?><br /> Your Postcode is: <?php echo $postcode; ?><br /> Your e-mail: <?php echo $email; ?><br /> <br /> <br /> Your age is: <?php echo $age; ?><br /> </body> </html> Later on i wish to also add a drop down field aswell to be validated that the right choice has been selected.. If anyone could help me or point me to a tutorial that deals with exactly this i would be most greatful. Link to comment https://forums.phpfreaks.com/topic/148369-php-form-numbers-validation/ Share on other sites More sharing options...
MatthewJ Posted March 7, 2009 Share Posted March 7, 2009 http://us3.php.net/is_numeric For a drop down, you usually just set a default field of "Select One" or something like that and make its value something you can check for like zero, so then you can use if($_POST['whatever'] == 0) { //Drop down item not selected } else { //Drop down item selected so do something with it } Link to comment https://forums.phpfreaks.com/topic/148369-php-form-numbers-validation/#findComment-778943 Share on other sites More sharing options...
pcw Posted March 7, 2009 Share Posted March 7, 2009 <?php $age = $_POST['age']; if ($age > 16 && $age < 26 ) { print "age ok"; } else { print "wrong age"; } print <<<END <form method="POST"> age: <input type="text" name="age"> <input type="submit" name="submit" value="submit"> END; ?> Thats how you would check the age is correct Link to comment https://forums.phpfreaks.com/topic/148369-php-form-numbers-validation/#findComment-778946 Share on other sites More sharing options...
redarrow Posted March 7, 2009 Share Posted March 7, 2009 strlen($data) == 0 <<< what you mean with this statement there no length? explain please. Link to comment https://forums.phpfreaks.com/topic/148369-php-form-numbers-validation/#findComment-778948 Share on other sites More sharing options...
saintryan Posted March 7, 2009 Author Share Posted March 7, 2009 strlen($data) == 0 <<< what you mean with this statement there no length? explain please. yeah if the string length is = to 0 then throw up an error message I am very new to php and followed at pho form guide... my updated myform code is as follows: <?php $firstname = check_input($_POST['firstname'], "Enter you firstname please"); $surname = check_input($_POST['surname'], "Enter you surnname please"); $addressone = check_input($_POST['adressone']); $postcode = check_input($_POST['postcode']); $email = check_input($_POST['email']); $age = check_input($_POST['age'], "Please enter your age"); if (is_numeric($age > 16 && $age < 26)) { return $age; } else { print ("age not valid"); } ?> <html> <body> Your first name is: <?php echo $firstname; ?><br /> Your Surname is: <?php echo $surname; ?><br /> Your address is: <?php echo $addressone; ?><br /> Your Postcode is: <?php echo $postcode; ?><br /> Your e-mail: <?php echo $email; ?><br /> <br /> <br /> Your age is: <?php echo $age; ?><br /> </body> </html> <?php function check_input($data, $error='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($error && strlen($data) == 0) { show_error($error); } else return $data; } function show_error($myError) { ?> <html> <body> <b>Please correct the following error:</b><br /> <?php echo $myError; ?> </body> </html> <?php exit(); } ?> i've included an if statement for the age question... but it still does not work? i think it needs to work with the check_input function i created... to bring back the custom error message if the age entered is incorrect, else return the data entered... i am still able to enter a string into the age field and it comes back. anyone please? Link to comment https://forums.phpfreaks.com/topic/148369-php-form-numbers-validation/#findComment-778982 Share on other sites More sharing options...
Philip Posted March 7, 2009 Share Posted March 7, 2009 if (is_numeric($age > 16 && $age < 26)) that is incorrect syntax if (is_numeric($age) && $age > 16 && $age < 26) Link to comment https://forums.phpfreaks.com/topic/148369-php-form-numbers-validation/#findComment-778993 Share on other sites More sharing options...
saintryan Posted March 7, 2009 Author Share Posted March 7, 2009 if (is_numeric($age > 16 && $age < 26)) that is incorrect syntax if (is_numeric($age) && $age > 16 && $age < 26) Hello Thanks for that. However the result still allows to bring back a string value.. i think i am doing something wrong with my check_input function?? and maybe the age check should be in there? but i'm not sure how? also if i put in a correct age... i get a blank screen?? thanks again Link to comment https://forums.phpfreaks.com/topic/148369-php-form-numbers-validation/#findComment-778996 Share on other sites More sharing options...
pcw Posted March 7, 2009 Share Posted March 7, 2009 You need to process the age differently <?php $age = $_POST['age']; if ($age == "") { print "Please enter your age" } elseif(is_numeric($age) && $age > 16 && $age < 26) { return $age; } else { print ("age not valid"); } ?> as its validity is different from check_input Link to comment https://forums.phpfreaks.com/topic/148369-php-form-numbers-validation/#findComment-778999 Share on other sites More sharing options...
Philip Posted March 7, 2009 Share Posted March 7, 2009 You wouldn't need to use the check_input function on the age check. You could also use intval() Link to comment https://forums.phpfreaks.com/topic/148369-php-form-numbers-validation/#findComment-779001 Share on other sites More sharing options...
saintryan Posted March 7, 2009 Author Share Posted March 7, 2009 Hi.. well ive just basically created another function called check_age which pretty much does the same job as the check data function exept i put my if statements in there so the code now looks like: <?php $firstname = check_input($_POST['firstname'], "Enter you firstname please"); $surname = check_input($_POST['surname'], "Enter you surnname please"); $addressone = check_input($_POST['adressone']); $postcode = check_input($_POST['postcode']); $email = check_input($_POST['email']); $age = check_age($_POST['age'], "Please enter your age"); ?> <html> <body> Your first name is: <?php echo $firstname; ?><br /> Your Surname is: <?php echo $surname; ?><br /> Your address is: <?php echo $addressone; ?><br /> Your Postcode is: <?php echo $postcode; ?><br /> Your e-mail: <?php echo $email; ?><br /> <br /> <br /> Your age is: <?php echo $age; ?><br /> </body> </html> <?php function check_age($age, $error='') { $data = htmlspecialchars($data); if (is_numeric($age) && $age > 16 && $age < 26) { show_error($error); } else return $age; } function check_input($data, $error='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($error && strlen($data) == 0) { show_error($error); } else return $data; } function show_error($myError) { ?> <html> <body> <b>Please correct the following error:</b><br /> <?php echo $myError; ?> </body> </html> <?php exit(); } ?> this kind of works.... but i am still able to enter a string into the age field and its gets through.... what am i doing wrong? am i on the right track? Link to comment https://forums.phpfreaks.com/topic/148369-php-form-numbers-validation/#findComment-779010 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.