Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Form validation

  1. #1
    New Coder
    Join Date
    Sep 2004
    Location
    Minnesota, USA
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form validation

    I want to make sure that forms that are being submitted have content. How do we check if a field is empty when it is submitted?

    I'm guessing

    Code:
    if (isset($_POST['item']))
    {
       do this ....
    }
    else
       echo 'error';
    What is the correct way of making sure a field is not empty?

  • #2
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    A note...

    this will result in 'Set' being echoed
    PHP Code:
    $var "";
    if (isset(
    $var)) {
      echo 
    "set";
    } else {
    echo 
    "not set";

    you might also check to see that the variable actually contains something

    ie..
    PHP Code:

    $var 
    $_POST['var'];

    if (
    strlen(trim($var)) <0) {
      
    // the variable is either empty or not set
    ] else {
      
    // there is something in the variable

    Lite...

  • #3
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Or, to make it easier for yourself, use the empty() function

    if(empty($_POST['item'])){
    echo 'Item is empty';
    }else{
    echo 'Item is filled';
    }

  • #4
    New Coder
    Join Date
    Sep 2004
    Location
    Minnesota, USA
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Looks good. Thanks!


  •  

    Posting Permissions

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