need help using 'if' and a '$_POST' with a form
Results 1 to 8 of 8

Thread: need help using 'if' and a '$_POST' with a form

  1. #1
    Senior Member Paul help!'s Avatar
    Join Date
    Aug 2013
    Location
    Manchester, England.
    Posts
    190

    need help using 'if' and a '$_POST' with a form

    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 Code:
    <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 Code:
    <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.
    Last edited by Paul help!; 03-05-2017 at 04:31 PM.

  2. #2
    Member
    Join Date
    Apr 2016
    Posts
    98
    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.
    Programming should not be a painful activity. If you are experiencing pain while programming, you are probably doing something wrong.

  3. #3
    Senior Member Paul help!'s Avatar
    Join Date
    Aug 2013
    Location
    Manchester, England.
    Posts
    190
    Thanks mate.

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

    Paul.

  4. #4
    Member
    Join Date
    Apr 2016
    Posts
    98
    Yes.
    Programming should not be a painful activity. If you are experiencing pain while programming, you are probably doing something wrong.

  5. #5
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    350
    PHP Code:

    <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

    PHP Code:
    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
    You need to believe in things that aren't true. How else can they become?― Terry Pratchett, Hogfather
    Blog

    Six month project

  6. #6
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,735
    I might use the is_numeric() function, so that things like -1.05 would also be trapped:
    PHP Code:
    if(!empty($_POST['fname'])) {
        if(
    is_numeric($_POST['fname'])) {
            
    // error message 
        
    }
        else {
            
    // continue processing the form
        
    }

    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  7. #7
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    8,440
    intval() can also be useful in similar situations, depending on whether you actually want any decimal/fractional numbers.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  8. #8
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,735
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •