tet3828 Posted January 20, 2008 Share Posted January 20, 2008 I am simply trying to verify that two fields in my form are not empty. But apparently I am not doing something right. Check it: if (isset($_POST['field1']) == FALSE AND if isset($_POST['field2']) == FALSE ) { //Tell the user both fields are empty, you suck try again. } Link to comment https://forums.phpfreaks.com/topic/86863-if-statement-driving-me-nuts/ Share on other sites More sharing options...
Stooney Posted January 20, 2008 Share Posted January 20, 2008 if(empty($_POST['field1']) && empty($_POST['field2'])){ //error } use || instead of && if you want to display the error if even 1 field is empty Link to comment https://forums.phpfreaks.com/topic/86863-if-statement-driving-me-nuts/#findComment-444028 Share on other sites More sharing options...
Fyorl Posted January 20, 2008 Share Posted January 20, 2008 You've used 'if' twice, you don't need it after the 'AND'. Also, as the above comment suggests, the variables may be set but still empty in which case you need to use the empty() function. Or just check if they are false with: if(!$_POST['field1'] && !$_POST['field2']) Link to comment https://forums.phpfreaks.com/topic/86863-if-statement-driving-me-nuts/#findComment-444057 Share on other sites More sharing options...
kenrbnsn Posted January 20, 2008 Share Posted January 20, 2008 The problem with using both the "empty()" test and the test for "false" is that these will give false positives when the field value is "0" (zero). The only way to really make sure the field is empty is to use the strlen() function. See the following example: <?php if (isset($_POST['submit'])) { echo '<pre>'. print_r($_POST,true) . '</pre>'; if(empty($_POST['field1']) && empty($_POST['field2'])) echo 'Both fields are empty<br>'; if(!$_POST['field1'] && !$_POST['field2']) echo 'Both fields are false<br>'; if(strlen(trim($_POST['field1'])) == 0 && strlen(trim($_POST['field2'])) == 0) echo 'Both fields are really empty<br>'; }?> <html> <head> <title>Test empty</title> </head> <body> <form method="post"> Field 1: <input type="text" name="field1"><br> Field 2: <input type="text" name="field2"><br> <input type="submit" name="submit"> </form> </body> </html> Try running this code putting the zero character into each field and see the results. Ken Link to comment https://forums.phpfreaks.com/topic/86863-if-statement-driving-me-nuts/#findComment-444061 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.