Death.System
Death.System

Reputation: 144

PHP if statement with checkbox error

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.

Upvotes: 1

Views: 1903

Answers (5)

Francis Lewis
Francis Lewis

Reputation: 8980

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

Upvotes: 1

keithhatfield
keithhatfield

Reputation: 3273

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();
            }
        }
    }
?>

Upvotes: 1

paulsm4
paulsm4

Reputation: 121649

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

Upvotes: 0

logiblocs
logiblocs

Reputation: 58

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

Hope this helps.

Upvotes: 0

Mike Purcell
Mike Purcell

Reputation: 19979

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

Upvotes: 1

Related Questions