0

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";
        }
    }
?>
8
  • Share your HTML code too. You can use array for taking checkbox value Commented Aug 26, 2014 at 6:13
  • try all equals as "===" Commented Aug 26, 2014 at 6:16
  • You can totally reduce this code ;) Commented Aug 26, 2014 at 6:16
  • 1
    instead of individual if you should use if .. else. Commented Aug 26, 2014 at 6:17
  • better solution is just do $pageCat = $_POST['catA'].$_POST['catB'].$_POST['catC'] Commented Aug 26, 2014 at 6:19

5 Answers 5

1

One simple (not perfect) solution:

$pageCat = (isset($_POST['catA']) ? $_POST['catA'] : '')
    . (isset($_POST['catB']) ? $_POST['catB'] : '')
    . (isset($_POST['catC']) ? $_POST['catC'] : '');
Sign up to request clarification or add additional context in comments.

1 Comment

@Veerendra If the checkboxes had the same names then I wouldn't of been able to set them individually and have the values saved. As previously stated, each if statement was overriding the one above if it was true.
0

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"
    )
)

Comments

0

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

Comments

0

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";
    }
}

Comments

0

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.

Comments

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.