最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Comparison operator != not working in <select> field

programmeradmin0浏览0评论
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 question

Please 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 question

Please 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
  • Your if statements were all smushed on to the one line for some reason, I've fixed that by reformatting the code so it's easier to read and understand. Also keep in mind your gender selection control may not pass legal muster in some legal jurisdictions, I recommend consulting legal advice. Some jurisdictions might offer a neutral gender, some may require that the user can opt out of identifying their gender. Some user might also consider nonesense offensive – Tom J Nowell Commented May 29, 2020 at 13:39
  • Thanks @tom i will replace the nonsense in my main project code, but then i know dkeeling method works atleast i can get the project running but then for a programmer i need to know why using != or !in_array() or anything ! Seems not to work with <select> – pandglobal Commented May 29, 2020 at 15:58
  • The select is irrelevant, once the value is submitted via POST there's no way to know how or where the value came from, just that it's a value with a name submitted via POST. 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
  • On closer inspection, this question has nothing to do with WordPress, it's a generic pure PHP question that just happens to be on a WordPress site. You should have asked it on stackoverflow – Tom J Nowell Commented May 29, 2020 at 20:50
Add a comment  | 

2 Answers 2

Reset to default 0

The 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';
  }
}
发布评论

评论列表(0)

  1. 暂无评论