Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 years ago.
Improve this questionPlease i want to ensure only male or female is selected in the dropdown if not fail the form but my validation keeps returning false even if male or female is selected.
Html
<select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Nonesense" selected>Nonesense</option>
</select>
Here is the php code
if ( isset( $_POST['submit'] ) ) {
if ( $_POST['gender'] != 'Male' || $_POST['gender'] != 'Female' ) {
echo 'sorry it must be either male or female'; return '';
}
}
Closed. This question is off-topic. It is not currently accepting answers.
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 years ago.
Improve this questionPlease i want to ensure only male or female is selected in the dropdown if not fail the form but my validation keeps returning false even if male or female is selected.
Html
<select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Nonesense" selected>Nonesense</option>
</select>
Here is the php code
if ( isset( $_POST['submit'] ) ) {
if ( $_POST['gender'] != 'Male' || $_POST['gender'] != 'Female' ) {
echo 'sorry it must be either male or female'; return '';
}
}
Share
Improve this question
edited May 29, 2020 at 13:36
Tom J Nowell♦
61.1k7 gold badges79 silver badges148 bronze badges
asked May 29, 2020 at 12:45
pandglobalpandglobal
431 silver badge9 bronze badges
4
|
2 Answers
Reset to default 0The reason it does not work is due to a logic error, which becomes obvious if you follow the code manually for each value It has nothing to do with the select
element, or special behaviour of !
or !=
So lets look at your conditional:
$_POST['gender'] != 'Male' || $_POST['gender'] != 'Female'
- if ( gender is not male ) OR ( gender is not female )
- then complain it must be male/female
So lets say that the gender is male:
- if ( false ) OR ( true )
- then complain it must be male/female
Since true is true, it resolves to if true
and the complaint appears.
So it appears, the problem is a misunderstanding of how OR
works
a OR b
<- this resolve to true if either 'a', 'b', or both a and b are true.
What I think you meant was:
if ( gender is male OR gender is female ) is not true
rather than
if ( gender is not male ) OR ( gender is not female )
But finally, this question and answer has nothing to do with WordPress.
The problem lies with your "or" operator. For example, even though "Male" may be selected, "Female" is not, which still makes the statement true as a whole.
I would do something like this:
$accepted_values = array('Male', 'Female');
if(isset($_POST['submit'])){
if(in_array($_POST['gender'],$accepted_values)) {
// Value is 'Male' or 'Female'
} else {
echo 'sorry it must be either male or female';
}
}
nonesense
offensive – Tom J Nowell ♦ Commented May 29, 2020 at 13:39select
is irrelevant, once the value is submitted viaPOST
there's no way to know how or where the value came from, just that it's a value with a name submitted viaPOST
. A bit like when you buy lego, you don't know if the lego was made in a mould, assembled by hand, or chisselled from a larger piece of plastic, and it doesn't matter.. If the answer from dkeeling answers your question you should mark it as the accepted answer, otherwise leave comments on that answer – Tom J Nowell ♦ Commented May 29, 2020 at 20:39