Beauford Posted May 10, 2009 Share Posted May 10, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/157593-need-help-checking-if-_post-filed-is-empty/ Share on other sites More sharing options...
daveh33 Posted May 10, 2009 Share Posted May 10, 2009 if (!$_POST['field-name']) { // The field has no value } else { // The field has a value } Quote Link to comment https://forums.phpfreaks.com/topic/157593-need-help-checking-if-_post-filed-is-empty/#findComment-831001 Share on other sites More sharing options...
Beauford Posted May 10, 2009 Author Share Posted May 10, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/157593-need-help-checking-if-_post-filed-is-empty/#findComment-831029 Share on other sites More sharing options...
wildteen88 Posted May 10, 2009 Share Posted May 10, 2009 You should do it like this if(isset($_POST['field_name_here']) && !empty($_POST['field_name_here'])) { // field has a value } Quote Link to comment https://forums.phpfreaks.com/topic/157593-need-help-checking-if-_post-filed-is-empty/#findComment-831032 Share on other sites More sharing options...
Beauford Posted May 10, 2009 Author Share Posted May 10, 2009 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/157593-need-help-checking-if-_post-filed-is-empty/#findComment-831197 Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 Well 0 is empty. 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/157593-need-help-checking-if-_post-filed-is-empty/#findComment-831206 Share on other sites More sharing options...
Beauford Posted May 12, 2009 Author Share Posted May 12, 2009 Well 0 is empty. 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? Quote Link to comment https://forums.phpfreaks.com/topic/157593-need-help-checking-if-_post-filed-is-empty/#findComment-832484 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/157593-need-help-checking-if-_post-filed-is-empty/#findComment-832487 Share on other sites More sharing options...
allworknoplay Posted May 12, 2009 Share Posted May 12, 2009 What about checking for NULL? $_POST['value'] == NULL Quote Link to comment https://forums.phpfreaks.com/topic/157593-need-help-checking-if-_post-filed-is-empty/#findComment-832489 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 What about checking for NULL? $_POST['value'] == NULL empty checks for that already. Quote Link to comment https://forums.phpfreaks.com/topic/157593-need-help-checking-if-_post-filed-is-empty/#findComment-832494 Share on other sites More sharing options...
radi8 Posted May 12, 2009 Share Posted May 12, 2009 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/157593-need-help-checking-if-_post-filed-is-empty/#findComment-832510 Share on other sites More sharing options...
wildteen88 Posted May 12, 2009 Share Posted May 12, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/157593-need-help-checking-if-_post-filed-is-empty/#findComment-832597 Share on other sites More sharing options...
Beauford Posted May 12, 2009 Author Share Posted May 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157593-need-help-checking-if-_post-filed-is-empty/#findComment-832604 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/157593-need-help-checking-if-_post-filed-is-empty/#findComment-832608 Share on other sites More sharing options...
Beauford Posted May 12, 2009 Author Share Posted May 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157593-need-help-checking-if-_post-filed-is-empty/#findComment-832625 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.