zack
zack

Reputation: 484

Checking multiple empty field in a form

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.

Upvotes: 0

Views: 263

Answers (3)

Kunal Gupta
Kunal Gupta

Reputation: 449

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.');

    }

Upvotes: 0

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

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)

Upvotes: 1

zack
zack

Reputation: 484

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.

Upvotes: 2

Related Questions