0

How can i check 2 text fields are empty with PHP.

Here is exactly what i want

there are 2 text fields in my form. I dont want the form to be submitted if both fields are empty. But if one of the text fields have a entered value form should get submitted.

I have tired this code but it wont submit if both fields values are entered.

if($_POST['inputOne'] == NULL AND $_POST['inputTwo'] == NULL )
    { 
        die('My Error Msg.');

    }

Can anyone tell me how to do this.

1
  • You want to use OR instead of AND.
    – Prix
    Sep 30, 2014 at 16:31

3 Answers 3

2

Thanks to Fred -ii- (with edits to he's code) i have found an answer

if(empty($_POST['inputOne']) AND empty($_POST['inputTwo']))

{ 
        die('My Error Msg.');

}

If you dont use AND it will not look for both fields empty together instead it will look of each field separately.

1
  • Actually your awesome. Sorry for the delay to vote :)
    – zack
    Sep 30, 2014 at 17:34
1
if($_POST['inputOne'] == NULL OR $_POST['inputTwo'] == NULL )

"But still this look for both fields to have a value. Adding value to one field will not post the form"

Use the following then. It will check if one or both are empty.

if(empty($_POST['inputOne']) || empty($_POST['inputTwo'])){...}

instead of using == NULL

FYI: || is the same as using OR


Or as you state in your answer: (using AND)

if(empty($_POST['inputOne']) && empty($_POST['inputTwo'])){...}

where && is the same as AND - just another quick "FYI".

  • It saves you a keystroke (wink)
0

There should be an OR instead of the AND.

if($_POST['inputOne'] == NULL OR $_POST['inputTwo'] == NULL )
    { 
        die('My Error Msg.');

    }

EDIT: If OR doesn't suffice try:

if(empty($_POST['inputOne']) || empty($_POST['inputTwo']))
    { 
        die('My Error Msg.');

    }
1
  • But still this look for both fields to have a value. Adding value to one field will not post the form
    – zack
    Sep 30, 2014 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.