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
}
10
  • $_POST[check_list] will be an array you can use Commented May 29, 2018 at 20:56
  • Have you tried using a switch statement php.net/manual/en/control-structures.switch.php Commented May 29, 2018 at 20:59
  • Do you think if use? Commented May 29, 2018 at 21:02
  • @smith see my update ,,, Commented May 29, 2018 at 21:09
  • if ($check =='2' && $check =='3' && $check =='6'){ how can $check be more than one thing at a time? Commented May 29, 2018 at 21:10

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

i think array_diff or array_intersect is probably better, but this will work so you can have my vote.
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.

4 Comments

what about if i have 2,3,6 ? how can use it in in_array ??
You can check this answer
@GutierrezPS comparing $_POST['check_list'] will not work as intended if other options are checked. use the empty function to check if $_POST['check_list'] has been set.
@abukotsh in_array(2, $array) && in_array(3, $array) && in_array(6, $array)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.