How to validate certain PHP values if (almost?) they are all received from the form as a string?

Asked

Viewed 45 times

1

I don’t know if they are all values, but in general, PHP takes everything as string when it comes from a form. Say I want to validate the age:

<form method="POST" action="test.php">
    <input type="text" name="age">
    <button type="submit">Manda</button>
</form>

And in test.php:

if(!is_int($_POST['age'])){
    echo 'não é integer';
    var_dump($_POST['age']);
}

You can see that using var_dump() I receive as string the information from "acts" and enters the if, even though I’m typing 14 in the form, for example. So, how to validate certain values in PHP if (almost?) all are received from the form as string?

Examples will be welcome.

1 answer

0

But what would you like to do exactly? PHP is kind of simplistic in this part. if you receive a variable that only has numbers and adds with another that only has numbers it will calculate normally as if it were integer for example

if($_POST['age'] >= 18){ echo "pessoa é maior de idade"; } 
  • as it is post field you need to check whether the variable contains something other than numbers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.