开发者

PHP Multiple Drop Down Variables Empty

I am performing some validation on three different drop down menus. If all three variables are NULL, I am wanting to direct the user to the specific error. I am asking the visitor to choose one of the dropdowns, not all three. He开发者_JAVA技巧re is my line of code that I cant see to get working:

if ( $dropdown1 == 'NULL', $dropdown2 == 'NULL', $dropdown3 == 'NULL' ) {
echo '<h1>Error Edited For the Sake of Brevity</h1>';
}
else 

Apreciate any thoughts anyone could share with me. Thanks Again, --Matt


Well, you're probably getting all of the values from $_REQUEST/$_GET/$_POST (I'm using $_REQUEST below because it works for all), for that you'd use isset.

if( !isset( $_REQUEST[ 'dropdown1' ] ) && 
    !isset( $_REQUEST[ 'dropdown2' ] ) && 
    !isset( $_REQUEST[ 'dropdown3' ] ) ) 
{
    // none of them are set.
}

If you're actually looking to see if the variables themselves are set to null, then you should use is_null

if( is_null( $dropdown1 ) && 
    is_null( $dropdown2 ) && 
    is_null( $dropdown3 ) )
{
    // none of them are set.
}

Finally, if you have reason to believe that they will be the STRING value 'NULL' then you can use == (this is a comparably rare circumstance):

if( 'NULL' == $dropdown1 && 
    'NULL' == $dropdown2 && 
    'NULL' == $dropdown3 )
{
    // none of them are set.
}

You can change any of the above to test to see if any of them are not set by using || (or) instead of && (and):

if( 'NULL' == $dropdown1 || 
    'NULL' == $dropdown2 || 
    'NULL' == $dropdown3 )
{
    // one of them is not set.
}


I assume you're trying to do a boolean AND comparison, which in PHP is:

if ( $dropdown1 == 'NULL' && $dropdown2 == 'NULL' && $dropdown3 == 'NULL' )...


if ( $dropdown1 == 'NULL', $dropdown2 == 'NULL', $dropdown3 == 'NULL' ) {

should be

if ( $dropdown1 == 'NULL' || $dropdown2 == 'NULL' || $dropdown3 == 'NULL' ) {

In this case if any of the variables is null, the it will display your error message


You shouldn't be using commas to separate the conditions

if ( $dropdown1 == 'NULL' || $dropdown2 == 'NULL' || $dropdown3 == 'NULL' )


if you want to give error message when all of the dropdowns are null then you should use this condition

if($dropdown1 == 'NULL' && $dropdown2 == 'NULL' && $dropdown3 == 'NULL' ){

echo $error_msg; }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜