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>