SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Feb 2009
    Location
    Arizona
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    General question about checking a form field

    I'm just curious which method you use to check the input is there, not how to validate or sanitize, but if you think 1 is better or why you use

    Code PHP:
    if(!isset($_POST['txtField1'])
    {
    echo "Sorry that is a required field";
    }

    vs

    Code PHP:
    if(empty($_POST['txtField1'])
    {
    echo "Sorry that is a required field";
    }

    it's just all about my making my code and clean and solid as possible.

    Thanks

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I use empty() for strings, which is both an isset() check and a length check.

    You can't use empty() for a field where 0 is a valid input.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should avoid empty() in this case. I use

    PHP Code:
    if ((!isset($_POST['txtField1']) || ($_POST['txtField1'] == ''))
    {
        echo 
    "Sorry that is a required field";



Bookmarks

Posting Permissions

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