Reputation: 484
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
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
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".
Upvotes: 1
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