user1879057
user1879057

Reputation:

Getting $_POST from checkboxes which are in loop

I'm trying to get checked values of my checkboxes after submit the form (something like validation), anyway independent from selected input my code can't decide which input was selected by user.

For example: Option 1 Option 2 Option 3

if Option 2,3 was selected by user, code's selecting Option 1,2. if Option 2 was selected by user, code's selecting Option 1. Only if user selected Option 1 or Option1, Option 2 or every options, then code works fine.

for ($y=0;$y<$n; $y++) {
    $caption_and_value = explode(":",$before_explode[$y]); 
    $caption = caption_and_value[0];
    $value = caption_and_value[1];
    $match .= '<input type="checkbox" name="'.$name_form[$y].'" value="'.$value.'"'; 
    if (isset($_POST[$name_form][$y])) {
        $match .= ' checked="checked"';
    } else {
        $match .= ''; 
    } 
    $match.='>'.$caption.'<br />';  
}

Pls let me know if I'm doing something wrong, I can't figured out this from couple of hours. Thanks!

Upvotes: 0

Views: 141

Answers (2)

Oki Erie Rinaldi
Oki Erie Rinaldi

Reputation: 1863

I have this one. Here's is the form :

<form method='POST' action = 'process.php'>
<?php 
for ($i=1;$i<=3;$i++){
echo "<input type='checkbox' name='box[]' value='$i'>$i<br>";
}
?>
<input type='submit' value='send'>
</form>

And it is the php :

    <?php 
    $input_value = $_POST['box'];
    $i = 0;
    $choice1 = NULL;
    $choice2 = NULL;
    $choice3 = NULL;
    foreach ($input_value as $value){
    if ($value == "1"){
    $choice1 = "active";
    }
    if ($value == "2"){
    $choice2 = "active";
    }
    if ($value == "3"){
    $choice3 = "active";
    }
    }
//choice checker script block
    if (!isset($choice1) and !isset($choice2) and !isset($choice3)){
    echo "You didn't choose anything!";
    }
    if ($choice1 == "active" and $choice2 == NULL and $choice3 == NULL){
    echo "You only chose 1";
    }
    if ($choice2 == "active" and $choice1 == NULL and $choice3 == NULL){
    echo "You only chose 2";
    }
    if ($choice3 == "active" and $choice2 == NULL and $choice1 == NULL){
    echo "You only chose 3";
    }
    if ($choice1 == "active" and $choice2 == "active" and $choice3 == NULL){
    echo "You chose 1 and 2";
    }
    if ($choice1 == "active" and $choice3 == "active" and $choice2 == NULL){
    echo "You chose 1 and 3";
    }
    if ($choice3 == "active" and $choice2 == "active" and $choice1 == NULL){
    echo "You chose 2 and 3";
    }
    if ($choice1 == "active" and $choice2 == "active" and $choice3 == "active"){
    echo "You chose all of the choice!";
    }
// end of choice checker script blok
    ?>

A little messy and profuse of code maybe. :D
But, if you want to check the choices that user chosen, i have already created 'choice checker script' above.
Actually you can change or replace if($choice == 'active') with if(isset($choice)), but it will return the same. Try it! you will know how to customize it..
I thinks that's what you are looking for.

Upvotes: 0

scrowler
scrowler

Reputation: 24406

You should use an array of checkboxes instead, much easier to deal with:

<input type="checkbox" name="boxes[]">
<input type="checkbox" name="boxes[]">
<input type="checkbox" name="boxes[]">

...then in PHP:

foreach($_POST['boxes'] as $key => $value) {
    echo $key . ': ' . $value;
}

Upvotes: 3

Related Questions