www.webdeveloper.com
Results 1 to 3 of 3

Thread: how to test for empty $_POST vars

  1. #1
    Join Date
    Mar 2010
    Posts
    281

    how to test for empty $_POST vars

    Hi,
    Im trying to test for empty post values. So far i'm just using this
    PHP Code:
    if($urlAdd == '') {return true
    i'm thinking this isn't enough. can someone recommend additional ways to test,

    at first i had this but it doesn't work. it always says it's set.

    PHP Code:
    if(isset($_POST['submit'])) {
        
    $urlAdd $_POST['url'];
        if((isset(
    $urlAdd)) || ($urlAdd !='')) {
            echo 
    "URL is set";
            
    //Validate URL
        
    } else {
            
    //echo "URL is not set";
            
    $error "URL is not set";
        }
    } else {
        echo 
    "submit is not set";
        } 
    thanks for the help

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,392
    I like to apply trim() to all $_POST values via array_walk_recursive(), then check for unset/empty fields via a combination of isset() and using the "is identical" operator to check for empty strings.
    PHP Code:
    array_walk_recursive(
      
    $_POST,
      
    create_function(
        
    '&$value, $key',
        
    '$value = trim($value);'
      
    )
    );
    if( ! isset(
    $_POST['foo']) or $_POST['foo'] === '') {
      
    // handle error for missing 'foo'

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2010
    Posts
    281
    awesome, thank you!

    not familiar with that, will try it out, thanks

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
  •  
HTML5 Development Center



Recent Articles