Hi guys.

I am at a stage in my learning where I am feeling confident enough to experiment and set myself little tasks while coding.

I am trying to do something which, in theory, should work perfectly fine but it doesn't.

Below is document 1:

<html>
<head>
</head>

<body>


<form action="practice1.php" method="post">
<br><label for="fname">First name:</label>
<input type="text" name="fname" value=""><br><br>



<input type="submit" value="send"><br><br>

</form>


</body>

</html>

..... this is just a simple form.

and below is document 2:

<html>
<head>
<title>Practice PHP</title>


</head>
<body>


<p>Firstname:</p>

<?php 

if(isset($_POST["fname"])) {

if(is_int($_POST["fname"])) {

echo "you must enter a sentence. Please try again";

} else {
echo $_POST["fname"];

}
}

 ?>


</body>
</html>

When the 'text field' in 'doc 1' has been filled in, and the info has been transfered to 'doc 2', I use a couple of if statements. The outer if statement works just fine, but the inner if statement will not function as I imagined.

Inside the inner 'if statement', I clearly check to see if the data type contained in the $_POST superglobal is an interger (by using the is_int() function).

But when I try to type an integer in the field and press send, I do not get the expected: "you must enter a sentence. Please try again" comment.

Instead, it just displays whatever I typed in, in the text field from 'doc 1'.

Can someone tell me where I am going wrong?

Paul.

    Is_int() tests the type of the variable, not what's in the variable. By definition, ALL $POST (and $GET and $_COOKIE) are string data types, regardless of what is in them.

    In general, to validate data, see the filter_var() statement and the Validate filters or use a regex match pattern.

      Thanks mate.

      Is preg_match() ..... a regular expression function I could use?

      Paul.

        
        <html>
        <head>
        <title>Practice PHP</title>
        
        
        </head>
        <body>
        
        
        <p>Firstname:</p>
        
        <?php 
        
        if(isset($_POST["fname"])) 
        {
        	$regex = "/^[A-Za-z]*$/";
        
        if (!preg_match($regex, $_POST["fname"])) 
        {
        	echo "you must enter a sentence. Please try again";
        } 
        else 
        {
        	echo $_POST["fname"];
        }
        }
        
         ?>
        
        
        </body>
        </html>
        
        

        That's a quick and easy way of doing it, as pbismad said you could use a var_filter

        if(filter_var($_POST['fname'] ,  FILTER_VALIDATE_REGEXP,["options"=> [ "regexp" =>  "/^[A-Za-z]*$/"]]) === FALSE)
        		{
        			echo "you must enter a sentence. Please try again";
        		}
        

        To give you an idea.

        A side note you should work on your indentation a little as well, I had the same problem not so long ago, once you get used to doing it you can read your code much easier, personally I favour Allman style. Going back to die now have fun 🙂

        https://en.wikipedia.org/wiki/Indent_style

          I might use the is_numeric() function, so that things like -1.05 would also be trapped:

          if(!empty($_POST['fname'])) {
              if(is_numeric($_POST['fname'])) {
                  // error message 
              }
              else {
                  // continue processing the form
              }
          }
          

            [man]intval/man can also be useful in similar situations, depending on whether you actually want any decimal/fractional numbers.

              Write a Reply...