1

Sorry for the dumb question, though I'm a little new to PHP. I've tried a lot of other ways to do this, but simply couldn't get this to work... Actually I want it to make it that I can have different functions attached to each checkbox that when a user selects a checkbox or another, and clicks the submit button, it triggers the specific functions. But I cannot get the checkboxes to work. It either works with only one selected, or if I check the 1st one then the 4th one, it outputs the 4th's code. Any other ways of doing this? Here is my attempt:

1.php

<form method="POST" action="2.php">
<input type="checkbox" name="test[]" value="test1" />test1<br />
<input type="checkbox" name="test[]" value="test2" />test2<br />
<input type="submit" name="submit" value="submit" />
</form>

2.php

$val = $_POST['test'];
if(isset($val)==true){
    for($i = 0 ; $i<count($val) ; $i++){
        if($val=='test1'){
            echo $val;
            die();
        }elseif($val=='test2'){
            echo $val;
            die();
        }else{
            echo "fail";
            die();
        }
    }
}else{
    return false;
}

Thank you.

2
  • 1
    Could you reformat your code with indentation? Really helps with readability.. Thx Commented Jan 12, 2012 at 19:36
  • 1
    tes2 should be test2 Commented Jan 12, 2012 at 19:37

5 Answers 5

1

Try:

$vals = $_POST['test'];
$valsCount = count($vals);

if ($valsCount > 0) {

    foreach ($vals as $val) {

        switch ($val) {

            case 'test1':
                echo $val;
                break;

            case 'test2':
                echo $val;
                break;

            default:
                echo 'Fail';
                break;
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh thanks! I never thought of using the "case" functions before.
1

As another option, if you're looking to call functions based on the value of the checkbox, you could do something like this ...

I've compressed it all into one file for simplicity, but this is the general idea ...

<form method="post" action="<?php echo $_SREVER['PHP_SELF']; ?>">
    <input type="checkbox" name="boxes[]" value="box1">Box 1</input><br />
    <input type="checkbox" name="boxes[]" value="box2">Box 2</input><br />
    <input type="checkbox" name="boxes[]" value="box3">Box 3</input><br />
    <input type="checkbox" name="boxes[]" value="box4">Box 4</input><br />
    <input type="checkbox" name="boxes[]" value="box5">Box 5</input><br />
    <input type="submit" value="Go!" />
</form>
<?php

    class boxProcessor
    {
        public function box1()
        {
            echo "<p>You've found box 1.</p>";
        }

        public function box2()
        {
            echo "<p>You've found box 2.</p>";
        }

        public function box3()
        {
            echo "<p>You've found box 3.</p>";
        }

        public function box4()
        {
            echo "<p>You've found box 4.</p>";
        }

        public function box5()
        {
            echo "<p>You've found box 5.</p>";
        }

    }

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $boxes = $_POST['boxes'];

        if(empty($boxes)){
            echo "<p>Nothing to do ...</p>";
        } else {
            $proc = new boxProcessor();

            foreach($boxes as $box){
                $proc->$box();
            }
        }
    }
?>

Comments

1

You're pretty close with your code as is, you just have to take into account array indexes.

for ($i = 0, $length = count($val); $i < $length; $i++)
{
    // add [$i] to $val to access an index of $val
    if ($val[$i] == 'test1')

4 Comments

What if I'm echoing checkboxes within foreach to output the number of values in the array? Will this result in similar conditions? Because in this one, I only have one elseif.
Your print_r statement is false. Value is a valid attribute for a checkbox. The posted array will only include values for checked boxes and will not have empty 'placeholders' for unchecked elements.
@Death.System, sorry, my first example was correct. Forgot what I said about value not being valid. I just changed the example back to the way it was, so once again, you were very close with your original code.
I'm getting a problem when selecting both test1 and test2. This had occurred a lot, basically, I changed the code from echo $val; to echo 'test1'; and echo 'test2'; But output is "test1" instead of test1 and test2.
0

Try changing the names from test1[] to test1 in 1.php and also check a typo on line 7- 2.php.

Hope this helps.

Comments

0

There are a couple of issues that probably don't have anything to do with the real problem:

1) You talk about "1st" and "4th" checkboxes ... but your code only shows two checkboxes

2) Your example misspells "tes2" (so PHP won't/can't find it)

3) You should probably get rid of all the "die()" clauses

SUGGESTION:

Check out this link:

http://www.html-form-guide.com/php-form/php-form-checkbox.html

1 Comment

Well my original code from before I had more than two checkboxes while looping through an array. I see the typo, but when fixing it, still occurs, also I've checked that tried it, couldn't get it to work. It was outputting the same thing two times. For the multiple checkboxes.

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.