mardi 29 août 2017

PHP For loop with checkboxes not giving expected output

I am getting some unexpected output from the following code and can't figure out what the problem is.

   <?
    if (isset($_POST['submit'])) {
    echo "<pre>";
    print_r($_POST);
    exit;
    }
    echo "<form action='test1.php' method='post'>";
    for ($i=0;$i < 10;$i++) {
    echo "<input type='text' value='$i' name='field[]'>";
    echo "<input type='hidden' name='cbox[]' value='0'>";
    echo "<input type='checkbox' value='1' name='cbox[]'><br>";
    }
    echo "<br><input type='submit' name='submit' value='go'>";
    ?>

If I run this, check on say number 4 and 6, I get this output:

Array
(
    [field] => Array
        (
            [0] => 0
            [1] => 1
            [2] => 2
            [3] => 3
            [4] => 4
            [5] => 5
            [6] => 6
            [7] => 7
            [8] => 8
            [9] => 9
        )

    [cbox] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
            [5] => 1
            [6] => 0
            [7] => 0
            [8] => 1
            [9] => 0
            [10] => 0
            [11] => 0
        )

    [submit] => go
)

Why is cbox not giving me a result of 1 next to the 4 and 6 as I would expect it?




Aucun commentaire:

Enregistrer un commentaire