条件逻辑形式验证

I'm having some issues writing logic for a script. I can't seem to wrap my head around it.

I have a form that contains 6 quantity fields, and two corresponding check boxes for each quantity field.

Quantity fields are referenced in variables $q1, $q2, $q3, $q4, $q5, $q6. Check boxes are referenced in variables $c1_1, $c1_2, $c2_1, $c2_2... etc.

The logic I want to achieve is, if you input a number into a quantity field, either one of the two corresponding checkboxes must be checked as well or the form invalidates.

My current code looks like this:

if(
   ($q1 !== "" && ($c1_1 == "" || $c1_2 == "")) ||
   ($q2 !== "" && ($c2_1 == "" || $c2_2 == "")) ||
   ($q3 !== "" && ($c3_1 == "" || $c3_2 == "")) ||
   ($q4 !== "" && ($c4_1 == "" || $c4_2 == "")) ||
   ($q5 !== "" && ($c5_1 == "" || $c5_2 == "")) ||
   ($q6 !== "" && ($c6_1 == "" || $c6_2 == ""))
) {
   $is_valid = false;
} else {
   $is_valid = true;
}

What's wrong with that code is that when I go to test that script, it requires both checkboxes to be checked to validate.

If I understood correctly, you want to invalidate if you meet one of this:

  • a field is empty
  • both checkboxes (belonging to a field) are unchecked

so it would be

if(
   ($q1 !== "" && ($c1_1 == "" && $c1_2 == "")) ||
   ($q2 !== "" && ($c2_1 == "" && $c2_2 == "")) ||
   ($q3 !== "" && ($c3_1 == "" && $c3_2 == "")) ||
   ($q4 !== "" && ($c4_1 == "" && $c4_2 == "")) ||
   ($q5 !== "" && ($c5_1 == "" && $c5_2 == "")) ||
   ($q6 !== "" && ($c6_1 == "" && $c6_2 == ""))
) {
   $is_valid = false;
} else {
   $is_valid = true;
}

with or without the inner ().

haven't tried myself, but should work.

I just tried a couple of things before submitting the answer. I wrote a form to replicate something similar to yours.

<form action="tester.php" method="POST">

    <input type="text" name="q1"/><br>
    <input type="checkbox" name="c1_1" value="Bike">check1<br>
    <input type="checkbox" name="c1_2" value="Car">check2<br><br>

    <input type="text" name="q2"/><br>
    <input type="checkbox" name="c2_1" value="Bike">check1<br>
    <input type="checkbox" name="c2_2" value="Car">check2<br><br>

    <input type="text" name="q3"/><br>
    <input type="checkbox" name="c3_1" value="Bike">check1<br>
    <input type="checkbox" name="c3_2" value="Car">check2<br><br>

    <input type="text" name="q4"/><br>
    <input type="checkbox" name="c4_1" value="Bike">check1<br>
    <input type="checkbox" name="c4_2" value="Car">check2<br><br>

    <input type="text" name="q5"/><br>
    <input type="checkbox" name="c5_1" value="Bike">check1<br>
    <input type="checkbox" name="c5_2" value="Car">check2<br><br>

    <input type="text" name="q6"/><br>
    <input type="checkbox" name="c6_1" value="Bike">check1<br>
    <input type="checkbox" name="c6_2" value="Car">check2<br><br>

    <input type="submit" name="submit" value="submit"/>

</form>

So then below is the php script

if(isset($_POST['submit']))
{
    $q1 = $_POST['q1'];


    $q2 = $_POST['q2'];


    $q3 = $_POST['q3'];


    $q4 = $_POST['q4'];


    $q5 = $_POST['q5'];


    $q6 = $_POST['q6'];


    if(
       ($q1 !== "" && (!isset($_POST['c1_1']) && !isset($_POST['c1_2']))) ||
       ($q2 !== "" && (!isset($_POST['c2_1']) && !isset($_POST['c2_2']))) ||
       ($q3 !== "" && (!isset($_POST['c3_1']) && !isset($_POST['c3_2']))) ||
       ($q4 !== "" && (!isset($_POST['c4_1']) && !isset($_POST['c4_2']))) ||
       ($q5 !== "" && (!isset($_POST['c5_1']) && !isset($_POST['c5_2']))) ||
       ($q6 !== "" && (!isset($_POST['c6_1']) && !isset($_POST['c6_2'])))
    ) 
    {
       echo 'false';
    } else 
    {
       echo 'true';
    }
}

Perhaps I am a bit late, but it seems the && is the issue. Also perhaps it's better to use the isset function, just my opinion.