ionfish
ionfish

Reputation: 165

looping through checkboxes in php, unexpected results

I am looping through a bunch of array type fields. I have two checkboxes with the names "taxable[]" and "ship[]"

I was entering these checkboxes into mysql and noticed that every other field in my form was submitting correctly except these.

if i have 6 rows of checkboxes and i uncheck the first row. the last row of checkboxes is affected.

similarly if i uncheck the first two rows, the last two rows is affected.

I broke this down into simple php below.. and looping through my checkboxes I see that the last iteration of the loop is where the checkboxes are not set, rather than the first loop. any ideas whats going on here. I also checked the size of the array, and it matches the number of fields for each type.

    echo sizeof($post["prodID"]);

    for($i = 0; $i<sizeof($post["prodID"]); $i++){         

        echo $i.' taxable  '.$post["taxable"][$i].' '.$i.' ship  '.$post["ship"][$i].'  <br />';

         continue;
    }

html for form:

<? foreach($products as $prod): ?>
                <tr class="<?= $prod["activeText"] ?>">
                    <td><?= $prod["productid"] ?></td>
                    <td><input type="text" name="name[]" value="<?= $prod["itemname"] ?>"/></td>
                    <td><textarea name="description[]"><?= $prod["description"] ?></textarea></td>
                    <td><input type="text" name="cost[]" value="<?= $prod["cost"] ?>"/></td>
                    <td><input type="text" name="price[]" value="<?= $prod["price"] ?>"/></td>
                    <?php $checked = ($prod["taxable"] == 1) ? "checked='checked'" : ""; ?>
                    <td style="padding:0px;"><input style="position:relative; right:-10px;"type="checkbox" name="taxable[]" <?= $checked ?>/></td>
                    <td>   
                        <select name="tax[]">
                            <?php foreach($taxes as $tax){ 
                                $selected = ($prod["inventoryTaxRateID"] == $tax["inventoryTaxRateID"]) ? 'selected="selected"' : '';
                             ?>
                            <option <?= $selected ?> value="<?= $tax["inventoryTaxRateID"] ?>"><?= $tax["name"] ?></option>
                            <?php } ?>
                        </select>
                    </td>
                    <?php $ship = ($prod["ship"] == 1) ? 'checked' : '' ?>
                    <td><input type="checkbox" name="ship[]" <?= $ship ?> /></td>
                    <td><input type="text" name="sandh[]" value="<?= $prod["shipping_handling"] ?>"/></td>

                    <td><?= $prod["activeText"] ?></td>
                    <td>
                        <? if($prod["inactive"] !== null): ?>
                            <input type="button" name="changeStatus" value="<?=  $prod["buttonText"] ?>" onclick="window.location='<?= base_url() ?>management/productManager/activate/<?= $prod["inventoryItemID"] ?>/<?= $prod["activeStatus"] ?>/<?= $category ?>'" />
                        <? endif; ?>
                        <input type="hidden" name="prodID[]" value="<?= $prod["inventoryItemID"] ?>" />
                        <input type="hidden" name="cat[]" value="<?= $prod["cat"] ?>" />
                    </td>
                </tr>
                <? endforeach; ?>

Upvotes: 0

Views: 2796

Answers (2)

user1121893
user1121893

Reputation: 11

You can add indexes to the input names which would probably solve your problem.

ie.

<input type="checkbox" name="ship[0]" value="ship1" />
<input type="checkbox" name="ship[1]" value="ship2" />
<input type="checkbox" name="ship[2]" value="ship3" />
<input type="checkbox" name="ship[3]" value="ship4" />

If you then select ship 1 and ship 3, $_POST['ship'][0] and $_POST['ship'][2] will be set, and the others will no be in the $_POST array since they were not submitted with the form.

You can then iterate through using array_keys($_POST['ship'])

ie.

if(is_array($_POST['ship']))
{
    foreach(array_keys($_POST['ship']) as $index)
    {
        echo $_POST['ship'][$index]."<br />";
    }
}

Which should echo all selected values

Upvotes: 1

piotrekkr
piotrekkr

Reputation: 3206

It's because PHP receive only selected checkboxes. Not selected checkboxes are not send by browser. Example

<input type="checkbox" name="ship[]" value="1" />
<input type="checkbox" name="ship[]" value="2" />
<input type="checkbox" name="ship[]" value="3" />
<input type="checkbox" name="ship[]" value="4" />

if you select first and last checkbox, browsers will only send first and last checkbox. PHP will see $_POST['ship'] as

Array(
    [0] => 1
    [1] => 4
)

Upvotes: 4

Related Questions