Danielle
Danielle

Reputation: 99

validation dropdown menu in php

I am trying to validate through php a form, but when I use dropdown it does not work when using the logical operator or "||"

<select name="options">
    <option value="">Seleccione idioma</option>
    <option value="Cake">Cake</option>
    <option value="Cookies">Cookies</option>
    <option value="Soda">Soda</option>
    <option value="Water">Water</option>
</select>

And this is my code in php

if($_POST['options'] != 'Cake' || $_POST['options'] != 'Cookies' || $_POST['options'] != 'Soda' || $_POST['options'] != 'Water' )

The code only works fine when in the "if" use only if($_POST['options']! = 'Cake')

Upvotes: 1

Views: 682

Answers (1)

Milan Chheda
Milan Chheda

Reputation: 8249

You should rather be using isset()

if(isset(if($_POST['options']))

OR

if($_POST['options'] != '')

OR

Have an array and use in_array

$array = ['Cake', 'Cookies', 'Soda', 'Water'];
if(in_array($_POST['options'], $array)){ // code goes here }

Upvotes: 1

Related Questions