Jump to content

Need help checking if $_POST filed is empty


Beauford

Recommended Posts

Sometimes the simplest things in PHP just baffle me:

 

I have a form with 12 fields. There are two fields that are optional.

 

If the user does enter something in these fields, I want to be able to check that and verify the information. If not, just move on with the rest of the script.

 

No matter what I do, even if nothing is entered in these fields, PHP thinks there is and tries to validate it. which screws everything up as there is really nothing there to validate.

 

I have tried if($_POST['value']) { do something }

I have tried if(!$_POST['value']) { do something }

I have tried if(isset($_POST['value'])) { do something }

I have tried if(!isset($_POST['value'])) { do something }

and a bunch of of stuff I was grasping at to get this to work.

 

This has got to be simple, but again, I have no clue.

 

The result I want is this. If there is a value, it has to be a number > than 0. Then I want to do a query on that number to make sure that it is in the DB. If it is, then the script continues, if not, an error is posted.

 

Thanks

 

B

Link to comment
Share on other sites

if (!$_POST['field-name']) {

 

// The field has no value

 

} else {

 

// The field has a value

 

}

 

Have tried that as per my posting, but I don't need to do anything if it is empty, so I need to check first to see if it has a value. Which for some reason always seems to, even if the user put nothing in the field.

 

thanks

Link to comment
Share on other sites

You should do it like this

if(isset($_POST['field_name_here']) && !empty($_POST['field_name_here']))
{
    // field has a value
}

 

This just gets more ridiculous. Now it doesn't think the 0 is a value or isn't numeric - and bypasses the check. See test code below.

 

if(isset($_POST['pfor']) && !empty($_POST['pfor'])) {

if(is_numeric($_POST['pfor'])) {

if($_POST['pfor'] == 0) {

$q=$db->query("SELECT userid, username FROM users WHERE userid={$_POST['pfor']}");

if($db->num_rows($q) == 0) {

$error = 1;

}

}

else {

$error = 1;

}

}

else {

$error = 1;

}

}

Link to comment
Share on other sites

Well 0 is empty. :D

 

Try this -

         if(!empty($_POST['pfor']) && $_POST['pfor'] !== 0) {
            if(is_numeric($_POST['pfor'])) {
               if($_POST['pfor'] == 0) {
                  $q=$db->query("SELECT userid, username FROM users WHERE userid={$_POST['pfor']}");
                  if($db->num_rows($q) == 0) {   
                     $error = 1;
                  }
               }
               else {
                  $error = 1;
               }
            }
            else {
               $error = 1;
            }         
         }

Link to comment
Share on other sites

Well 0 is empty. :D

 

Try this -

         if(!empty($_POST['pfor']) && $_POST['pfor'] !== 0) {
            if(is_numeric($_POST['pfor'])) {
               if($_POST['pfor'] == 0) {
                  $q=$db->query("SELECT userid, username FROM users WHERE userid={$_POST['pfor']}");
                  if($db->num_rows($q) == 0) {   
                     $error = 1;
                  }
               }
               else {
                  $error = 1;
               }
            }
            else {
               $error = 1;
            }         
         }

 

Thanks for the info and the code, but it still doesn't work. and not to get into a long discussion, but to me 0 is NOT empty. It takes up space, bytes, memory, or whatever else to display it, and this is what I want to check for. The only time it should bypass the check is if there is nothing there. The user entered nothing. 0 is something. Can PHP not do this simple function?

 

 

Link to comment
Share on other sites

Well PHP's empty function treats 0 as empty.

 

Try this -

         if(!empty($_POST['pfor']) && intval($_POST['pfor']) !== 0) {
            if(is_numeric($_POST['pfor'])) {
               if($_POST['pfor'] == 0) {
                  $q=$db->query("SELECT userid, username FROM users WHERE userid={$_POST['pfor']}");
                  if($db->num_rows($q) == 0) {   
                     $error = 1;
                  }
               }
               else {
                  $error = 1;
               }
            }
            else {
               $error = 1;
            }         
         }

Link to comment
Share on other sites

Well it seems as though the following is invalid because the second test for 0 can never occur:

<?php
if(!empty($_POST['pfor']) && intval($_POST['pfor']) !== 0) {
            if(is_numeric($_POST['pfor'])) {
               if($_POST['pfor'] == 0) {
                  $q=$db->query("SELECT userid, username FROM users WHERE userid={$_POST['pfor']}");
                  if($db->num_rows($q) == 0) {   
                     $error = 1;
                  }
               }
               else {
                  $error = 1;
               }
            }
            else {
               $error = 1;
            }         
         }
?>

 

Try this instead:

<?php
if(isset($_POST['pfor']) && (is_numeric($_POST['pfor']) || intval($_POST['pfor']) == 0)) {
    if($_POST['pfor'] == 0) {
        $q=$db->query("SELECT userid, username FROM users WHERE userid={$_POST['pfor']}");
        if($db->num_rows($q) == 0) {   
            $error = 1;
        }
        else{
            // Record-set retrieved, do something totally cool here
        }
    }
    else {
        $error = 1;
    }
}
else {
    $error = 1;
} 
?>

Link to comment
Share on other sites

PHP sees zero with two meanings an integer and a boolean (false). You should also note that all $_POST data are strings.

 

So if you want to see if the user only entered a zero in the field use

if($_POST['pfor'] == '0')

 

or type cast

$pfor = (int) $_POST['pfor'];
if($pfor === 0)

 

Link to comment
Share on other sites

Lets go back to the beginning. All I want to do is see if the user put something in $_POST['pfor'].

 

This could be a 'z', a 'q' a > a ? a 9 or the 0.  If the user has entered nothing then just move on with the rest of the script, if there is a value then it gets checked.

 

I just find it unbelievable that PHP can not handle this seemingly simple function.

 

thanks for all the input.

 

 

Link to comment
Share on other sites

         if(!empty($_POST['pfor']) || intval($_POST['pfor']) == 0) {
            if(is_numeric($_POST['pfor'])) {
               if($_POST['pfor'] == 0) {
                  $q=$db->query("SELECT userid, username FROM users WHERE userid={$_POST['pfor']}");
                  if($db->num_rows($q) == 0) {   
                     $error = 1;
                  }
               }
               else {
                  $error = 1;
               }
            }
            else {
               $error = 1;
            }         
         }

Link to comment
Share on other sites

This is how I believe I have got around this.

 

in the form I do this.

 

<td><input type='text' name='pfor' value=''></td>

 

For the check I do this.

 

if($_POST['pfor'] != "") {

    Check the rest of it

}

 

Not my first choice, but workable.

 

Thanks again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.