Problem Validating

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Knb15
Forum Newbie
Posts: 2
Joined: Fri Apr 16, 2010 1:16 pm

Problem Validating

Post by Knb15 »

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:
<body>

<form action="example04.php" method="post" >
Enter your age: <input name="age" size="2">
<input type="submit" name="submit" value="Go">
</form>

</body>
PHP Code:

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/>";
}
?>
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
adityamenon90
Forum Newbie
Posts: 7
Joined: Fri Apr 16, 2010 1:41 pm

Re: Problem Validating

Post by adityamenon90 »

Hi, please try this code:

Code: Select all

<?php
        if(!empty($age)) //check if the age field is empty
        {
              if(is_numeric($age)) //check if user has given alphabets as age
              {
                   echo 'Welcome.';
               }
               else
               {
                     echo 'Please enter a valid age.';
                }
        }
        else
        {
              echo 'The age field is required.';
         }
?>
Knb15
Forum Newbie
Posts: 2
Joined: Fri Apr 16, 2010 1:16 pm

Re: Problem Validating

Post by Knb15 »

Thanks for your reply.

However, your suggestion solved the problem of when the field is empty, but "broke" another problem...of when something other than a number is input.

It seems that it makes it through the first IF statement, and then is able to go through the nested IF statement...but it does not see the nested else statement, and goes right for the last ELSE statement.

I tried a few other combinations, and some other synthax in combination with what you suggested but nothing.
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Problem Validating

Post by Reviresco »

Make sure you're getting the value of age:

Code: Select all


$age = $_POST['age'];

if(!empty($age)) //check if the age field is empty
        {
              if(is_numeric($age)) //check if user has given alphabets as age
              {
                   echo 'Welcome.';
               }
               else
               {
                     echo 'Please enter a valid age.';
                }
        }
        else
        {
              echo 'The age field is required.';
         }
Post Reply