Problem Validating
Posted: Fri Apr 16, 2010 1:24 pm
Hi,
I'm new to PHP, and have been searching forums to get some help with form validating and sanitizing.
I got a simple form validating code from a tutorial, and tried it just to get started.
The code works, but one part of it does not. I would like to know if i am doing anything wrong, or if perhaps there's a syntax error in the code that i could not find.
HTML Form:
Basically, when the input is null, or nothing is written, it is not returning "The age field is required." Instead it is returning "Please enter a valid age."
However, if i enter a valid age or if i enter an invalid character, the proper response is returned.
Probably something very minor that i'm not familiar with yet, but i appreciate it anyway!
Thanks
Knb15
I'm new to PHP, and have been searching forums to get some help with form validating and sanitizing.
I got a simple form validating code from a tutorial, and tried it just to get started.
The code works, but one part of it does not. I would like to know if i am doing anything wrong, or if perhaps there's a syntax error in the code that i could not find.
HTML Form:
PHP Code:<body>
<form action="example04.php" method="post" >
Enter your age: <input name="age" size="2">
<input type="submit" name="submit" value="Go">
</form>
</body>
Code: Select all
<?php
if (!filter_has_var(INPUT_POST, 'submit')) {
echo "form";
// include the form.
}
$age = filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT);
if (is_null($age)) {
echo "The 'age' field is required.<br />";
} elseif ($age === FALSE) {
echo "Please enter a valid age.<br />";
} else {
echo "Welcome.<br/>";
}
?>However, if i enter a valid age or if i enter an invalid character, the proper response is returned.
Probably something very minor that i'm not familiar with yet, but i appreciate it anyway!
Thanks
Knb15