0x1801ce
0x1801ce

Reputation: 692

Using multiple checkboxes for the single variable?

What I am trying to achieve here is giving the user the ability to select one or all of the categories that a page will display on. The user would check any combination of 3 checkboxes and a variable will be computed and saved to the database. The values are then retrieved from the database and the checkboxes are echo'd checked accordingly. What's happening is when more then 1 checkbox is checked only the latter value is saved e.g. if A and B are checked, only B is saved, or when A, B and C are checked, only C is saved. If just 1 checkbox is saved it's fine.

The PHP

<?php
    if(isset($_POST['submit'])){
        if ($_POST['catA'] == "a" && $_POST['catB'] == "b" && $_POST['catC'] == "c"){
            $pageCat = "abc";
        }

        if ($_POST['catA'] == "a" && $_POST['catB'] == "b"){
            $pageCat = "ab";
        }

        if ($_POST['catA'] == "a" && $_POST['catC'] == "c"){
            $pageCat = "ac";
        }

        if ($_POST['catB'] == "b" && $_POST['catC'] == "c"){
            $pageCat = "bc";
        }
            if ($_POST['catA'] == "a"){
        $pageCat = "a";
        }

        if ($_POST['catB'] == "b"){
            $pageCat = "b";
        }

        if ($_POST['catC'] == "c"){
            $pageCat = "c";
        }
    }
?>

Upvotes: 0

Views: 1649

Answers (5)

C4d
C4d

Reputation: 3282

Sadly I cant see the form-part of your code.
Just to notice: Remember setting up different names for your checkboxes!

Here's a short example how to do it:

// 4 Checkboxes with diffrent names //
echo '<form name="form1" method="post" action="test_scroll.php">
        A<input type="checkbox" name="checkbox1" value="a">
        B<input type="checkbox" name="checkbox2" value="b">
        C<input type="checkbox" name="checkbox3" value="c">
        D<input type="checkbox" name="checkbox4" value="d">
        <br><br>
        <input type="submit" name="submit" value="submit"/><br><br><br>';

// Loop throught the post-params and append the values of the checkboxes //
    array_pop($_POST);
    foreach($_POST as $entry){
        $values .= $entry;
    }
// here comes the result //
    echo $values;

The array_pop just deletes the last entry of the array which is "submit". So the values of the checkboxes are left.

If checkbox A and B are submitted the result will just be "ab". This example only works for a pure checkbox-form. If there are more values you have to pop them out before using this looping-method.

Greetings.

Upvotes: 0

Class
Class

Reputation: 3160

to overcome this you could just use if/elseif's or you could do something posted below because if a & b are checked it goes down the list of ifs and replaces with one that is true.

With A && B

if ($_POST['catA'] == "a" && $_POST['catB'] == "b" && $_POST['catC'] == "c") FALSE
if ($_POST['catA'] == "a" && $_POST['catB'] == "b") TRUE $pageCat = 'ab'
if ($_POST['catA'] == "a" && $_POST['catC'] == "c") FALSE $pageCat = 'ab'
if ($_POST['catB'] == "b" && $_POST['catC'] == "c") FALSE $pageCat = 'ab'
if ($_POST['catA'] == "a") TRUE $pageCat = 'a'
if ($_POST['catB'] == "b") TRUE $pageCat = 'b'
if ($_POST['catC'] == "c") FALSE $pageCat = 'b'
Final value of pageCat = 'b'

try:

if (isset($_POST['submit'])) {
    $pageCat = '';
    if($_POST['catA'] === "a"){
        $pageCat .= "a";
    }
    if($_POST['catB'] === "b"){
        $pageCat .= "b";
    }
    if($_POST['catC'] === "c"){
        $pageCat .= "c";
    }
}

Upvotes: 0

dashtinejad
dashtinejad

Reputation: 6253

One simple (not perfect) solution:

$pageCat = (isset($_POST['catA']) ? $_POST['catA'] : '')
    . (isset($_POST['catB']) ? $_POST['catB'] : '')
    . (isset($_POST['catC']) ? $_POST['catC'] : '');

Upvotes: 1

Veerendra
Veerendra

Reputation: 2622

For example in such case you can use and array to get multiple values

<!DOCTYPE html>
<html>
<body>

<form action="">
<input type="checkbox" name="vehicle[]" value="Bike">I have a bike<br>
<input type="checkbox" name="vehicle[]" value="Car">I have a car 
</form>

</body>
</html>

So if in the above case user selected both the values then you will get the below in the post

 <?php 
     echo "<pre>";print_r($_POST['vehicle']);
    ?>

it will give you output

Array( [0] => Bike, [1] => Car )

And then you can process it the way you want

Upvotes: 0

Justinas
Justinas

Reputation: 43479

To have multiple entries of same variable, make it's name array:

<input type="checkbox" value="ACat" name="Category[A]"/>
<input type="checkbox" value="BCat" name="Category[B]"/>
<input type="checkbox" value="CCat" name="Category[C]"/>

Your POST will be like this:

array(
    "Category" => array(
        "A" => "ACat"
        "B" => "BCat"
        "C" => "CCat"
    )
)

Upvotes: 0

Related Questions