0

I have this form :

<form action="" method="post">
<input type="checkbox" name="check_list[]" value="0" id="check_list">- 0<br>
<input type="checkbox" name="check_list[]" value="1" id="check_list">- 1<br>
<input type="checkbox" name="check_list[]" value="2" id="check_list">- 2<br>
<input type="checkbox" name="check_list[]" value="3" id="check_list">- 3<br>
<input type="checkbox" name="check_list[]" value="4" id="check_list">- 4<br>
<input type="checkbox" name="check_list[]" value="5" id="check_list">- 5<br>
<input type="checkbox" name="check_list[]" value="6" id="check_list">- 6<br>
<input type="checkbox" name="check_list[]" value="7" id="check_list">- 7<br><br>
<input type="submit" name="submit" Value="Submit"/>
</form>

Now I wont to check the result from this form ,, if result have (2,3,6) we need to do something, and if result have value (2,3) we need to do something, and if result have value (2) we need to do something, and if result have value (3) we need to do something,and if result have value (6) we need to do something,

How can do that in php ?

I try this but not work good with me

if(!empty($_POST['check_list'])) {

    foreach($_POST['check_list'] as $check) {
    if ($check =='2' && $check =='3' && $check =='6'){
        // do something
    } elseif ($check =='2' && $check =='3'){
        // do something
    } elseif ($check =='6'){
        // do something
    } elseif ($check =='3'){
        // do something
    } elseif ($check =='2'){
        // do something
    }
}
}

Also i try this and not do any thing

if(in_array(array(2,3,6),$_POST['check_list'])){ 
// not work
} elseif(in_array(array(2,3),$_POST['check_list'])){ 
// not work
} elseif(in_array(array(2),$_POST['check_list'])){ 
// work
} elseif(in_array(array(3),$_POST['check_list'])){ 
// work
} elseif(in_array(array(7),$_POST['check_list'])){ 
// work
}
abukotsh
  • 45
  • 7

2 Answers2

1

In short, when you submit the form you'll have an array $_POST['check_list'] that contains the elements you've checked. So, if you check 2, 3 and 6, $_POST['check_list'] = [2, 3, 6]. Note that if you don't check any options, $_POST['check_list'] will not exist.

Then you can use if (in_array(2, $_POST['check_list'])) {...} to do what you want if 2 is checked, for example.

Gutierrez PS
  • 227
  • 2
  • 10
1
    <?php

if(!empty($_POST['check_list'])){
    if(
        has_values(Array(2,3,6), $_POST['check_list'])
    ){
        //Do what you need to do

    }else if(
        has_values(Array(2,3), $_POST['check_list'])
    ){
        //Do what you need to do

    }else if(
        has_values(Array(2), $_POST['check_list'])
    ){
        //Do what you need to do

    }
}else{
    //no checkboxes have been set
}

function has_values($testValues, $arrValues){
/*
    testValues is a 1 dimensional array (needles)
    arrValues is a 1 dimensional array that takes the array of set checkboxs ( haystack )
*/
    foreach ($testValues as $key => $value) {
        if(!in_array($value, $arrValues)){
            return false;
        }
    }
    return true;
}

Developer notes in_array is not type dependent.

if a checkbox is not checked the $_POST['check_list'] index will changes

Plixxer
  • 466
  • 4
  • 15
  • i think `array_diff` or `array_intersect` is probably better, but this will work so you can have my vote. –  May 29 '18 at 21:30