简体   繁体   中英

PHP if/else and AND/OR to check mandatory and optional fields

I have eight input fields in my form that I need to check whether they are empty. Four of these are text fields, the other four are list boxes. When the user submits the form, the four text boxes need to be filled. The fifth field is a Yes/No selection. If the selection in this field is "No", then the remaining three can be left blank. However, if the selection in the fifth field is "Yes" then the last three also have to be selected using the list boxes.

Let us call the first four (textboxes) "mandatory-text1", "mandatory-text2", "mandatory-text3", and "mandatory-text4", the Yes/No selection list box "yes-no" and the optional selections "a", "b" and "c".

SO:

If mandatory-text1, or mandatory-text2, or mandatory-text3, or mandatory-text4, are blank, the processing php will abort.

However, if mandatory-text1, mandatory-text2, mandatory-text3, and mandatory-text4, are all filled in, the next check is to see whether the yes-no contains a "Yes" or a "No".

  • If it contains a No, then a, b and c have to be necessarily left blank.
  • If it contains a Yes, then a, b and c (list boxes) have to be necessarily selected.

With my level of proficiency in PHP, I have been able to reach this far:

    if ((mandatory-text1 || mandatory-text2 || mandatory-text3 == "") OR (yes-no == "Yes" AND (!(a || b || c  == ""))) {
    echo "blah" ;
    exit();
    }
    else {
    //do some stuff
    }

I haven't coded the 'do some stuff' part yet because when I try this code, I get the 'blah' message even when all the text fields are filled in, the yes-no is "Yes" and a, b and c have been selected.

I tried it first with a simple "||" for all the fields and that works fine - ie if even one of the fields is left blank I get the 'blah' message. I have also tried just checking for the yes-no and a,b,c and that too works fine ie if there is a Yes in the yes-no i get the blah if the a,b,c fields are blank.

But I need to satisfy all the conditions before I can proceed to the next step. The posts I have read here on SO brought me to the stage I am in right now. But none went to the level I want for my project.

Any hints for the logic would be appreciated!

Okay first of all, you are supposed to use $ before variable names

Second, mandatory-text1||mandatory-text2||mandatory-text3=="" is wrong logic. It should be $mandatory-text1=""||$mandatory-text2==""|| ... $mandatory-text1=""||$mandatory-text2==""|| ...

It works because if(mandatory-text1) alone means if mandatory-text1 is defined and not empty or null string, so in this case $mandatory-text1||$mandatory-text2||$mandatory-text3 alone is sufficient

if ((mandatory-text1 || mandatory-text2 || mandatory-text3 == "") OR (yes-no == "Yes" AND (!(a || b || c  == ""))) {
echo "blah" ;
exit();
}
else {
//do some stuff
}

same for a||b||c

(mandatory-text1 || mandatory-text2 || mandatory-text3 == "")

This isn't working the way you think it is. This says:

if mandatory text 1 is not empty or mandatory 2 text is not empty or mandatory 3 text is equal to ''.

Try:

(mandatory-text1 == "" || mandatory-text2 == "" || mandatory-text3 == "")

You may redesign your control in a more readable way, with a function or code block per field type, for example :

<?php


function checkFields( $data )
{
    // List of field to check
    $fields = array( 'mandatory-text1', 'mandatory-text2', 'mandatory-text3', 'mandatory-text4', 'a', 'b', 'c' );

    foreach ($fields as $field )
    {
        switch( $field )
        {
            case 'mandatory-text1':
            case 'mandatory-text2':
            case 'mandatory-text3':
            case 'mandatory-text4':
                if ( $data[$field] == '' )
                {
                     return false;
                }
            break;


            case 'a':
            case 'b':
            case 'c':
                if ( $data['yes-no'] == 'yes' && $data[$field] == '' )
                {
                    return false;
                }
                elseif( $data['yes-no'] == 'no' && $data[$field] != '' )
                {
                    return false;
                }
            break;
        }
    }

    // If we have not returned false so far, then all is clear
    return true;
}

// Usage :
if( checkFields( $_POST ) )
{
    // every thing is ok
}
else
{
    // bad submission
}
?>

You should mostly avoid 2 things here :

  • Long AND / OR statements
  • Repeating your self with the smae controls

If I understood correctly, something like this should work. Do NOT use dashes in variable names.

if ($mandatory1 && $mandatory2 && $mandatory3 && $mandatory4)
{
    if ($choice == "Y" && $a && $b && $c)
    {
        // do whatever
    }
    elseif ($choice == "N" && !$a && !$b && !$c)
    {
        // do something else
    }
}
else
{
    // submission incorrect
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM