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> Code (markup): 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> Code (markup): 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.
is_int That's what you're looking for. To do a range check just do something like: if($number > 12 && $number < 16){ } else { }
if(is_numeric ($number ) && ($number > 12) && ($number < 16)){ } else { } To create a drop-down box, you can use the follow HTML lines: <select name="your_box_name"> <option value="your 1st value">Your 1st title</option> <option value="your 2st value">Your 2st title</option> ... </select>
PHP code for your drop down box: echo ("<Select name='age'>"); for ($i=12; $i<=16;$i++) { echo ("<option value='{$i}'>{$i}</option>"); } echo ("</select>");
Hello again. yeah i'm quite new to php and followed an online form tutorial.. and just wanted to add more code to adapt it some more. i have added a function called "is_numeric".. however.. it can still return strings.. and i dont even know if i'm putting the is_numeric if statement in the right place... i thought it was meant to go in my check_input function?? would be a great help if someone could look at my code and tell me where i am going wrong? thanks. <?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(); } ?> Code (markup):
$age = intval($_POST['vage']); if (is_int($age)) { if ((16 < $age) && (26 > $age)) { echo 'age not valid<br />'; } } else { echo 'age not valid<br />'; } PHP: Not sure on the > < portions are correct, because I was never good at greater then less then in math class, but you get the idea from above. Using the int and is_int php functions.
For the age validation, you can just use this: $age = intval($_POST['age']); if($age > 16 && $age < 26) { echo "Age input is valid"; } else { echo "Age input is not valid"; } PHP: