How do you test for blank form input?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris  Portka

    How do you test for blank form input?

    This is a very simple question but after a long time I still can't find
    an answer looking on my own. How do you test for no user input in a
    form? Example:

    <form action="index.p hp" method="get">
    Type your first name:
    <input type="text" name="FirstName ">
    <input type="submit" value="Submit">
    </form>

    <?php
    if ($_GET["FirstName"] == "")
    {
    echo "No user input";
    }
    ?>

    Is this the correct way to always test if there was nothing input by
    the user in the form? Should I test for nil or something else?
    Thanks,
    Chris

  • black francis

    #2
    Re: How do you test for blank form input?

    try

    if (trim($var) == "") //empty
    else //not empy

    Comment

    • Lisa Pearlson

      #3
      Re: How do you test for blank form input?

      trim() and also look at empty()

      "Chris Portka" <chrisportka@gm ail.com> wrote in message
      news:1124330068 .912666.7920@g4 3g2000cwa.googl egroups.com...[color=blue]
      > This is a very simple question but after a long time I still can't find
      > an answer looking on my own. How do you test for no user input in a
      > form? Example:
      >
      > <form action="index.p hp" method="get">
      > Type your first name:
      > <input type="text" name="FirstName ">
      > <input type="submit" value="Submit">
      > </form>
      >
      > <?php
      > if ($_GET["FirstName"] == "")
      > {
      > echo "No user input";
      > }
      > ?>
      >
      > Is this the correct way to always test if there was nothing input by
      > the user in the form? Should I test for nil or something else?
      > Thanks,
      > Chris
      >[/color]


      Comment

      • Marcin Dobrucki

        #4
        Re: How do you test for blank form input?

        Chris Portka wrote:
        [color=blue]
        > <?php
        > if ($_GET["FirstName"] == "")
        > {
        > echo "No user input";
        > }
        > ?>[/color]

        how about:

        if (isset($_GET['FirstName']) && empty($_GET['FirstName'])) {
        echo "yes, it's empty";
        }

        Specifically the "empty" function will be useful here, but sometimes its
        good to check that the variable actually exists.

        /Marcin

        Comment

        • Smitro

          #5
          Re: How do you test for blank form input?

          Chris Portka wrote:[color=blue]
          > This is a very simple question but after a long time I still can't find
          > an answer looking on my own. How do you test for no user input in a
          > form? Example:
          >
          > <form action="index.p hp" method="get">
          > Type your first name:
          > <input type="text" name="FirstName ">
          > <input type="submit" value="Submit">
          > </form>
          >
          > <?php
          > if ($_GET["FirstName"] == "")
          > {
          > echo "No user input";
          > }
          > ?>
          >
          > Is this the correct way to always test if there was nothing input by
          > the user in the form? Should I test for nil or something else?
          > Thanks,
          > Chris
          >[/color]
          I also use NULL some times...eg.
          if ($_GET["FirstName"] == NULL)

          Comment

          • BKDotCom

            #6
            Re: How do you test for blank form input?

            if ( !isset($_GET['whatever']) || ( empty($_GET['whatever']) &&
            $_GET['whatever'] != 0) )

            Comment

            • Marcin Dobrucki

              #7
              Re: How do you test for blank form input?

              BKDotCom wrote:[color=blue]
              > if ( !isset($_GET['whatever']) || ( empty($_GET['whatever']) &&
              > $_GET['whatever'] != 0) )[/color]

              php.net/empty

              "... As of PHP 4, The string value "0" is considered empty..."

              Comment

              Working...