too long

This question already has an answer here:

I would like to send inputs and checkboxes values via POST method to php page for updating myql database table, the problem is that I cannot receive uncheked chechboxes, please look at the code below :

index.html :

<form id='form1' method='post' action='update.php'>
    <input type='hidden' name='id[]' value='1'>
    <input type='text' name='firstName[]' value='John'>
    <input type='checkbox' name="married[]" value='1' checked>
    <br/>
    <input type='hidden' name='id[]' value='2'>
    <input type='text' name='firstName[]' value='Linda'>
    <input type='checkbox' name="married[]" value='1'>
    <br/>
    <input type='hidden' name='id[]' value='3'>
    <input type='text' name='firstName[]' value='Mercedes'>
    <input type='checkbox' name="married[]" value='1' checked>
    <br/>
    <input type='button' name='submit' value='update'>
</form>

update.php

<?php
    print_r($_POST);
?>

The content of the $_POST is :

Array
(
[id] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
    )

[firstName] => Array
    (
        [0] => 'John'
        [1] => 'Linda'
        [2] => 'Mercedes'
    )

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

Like you see the 'married' fields is only received when checkboxes are checked, mean that Linda will be ignored, and mercedes will get the value 1 (married) of Mercedes.

How Can I resolve this?

</div>

The quick solution is to give the fields explicit indexes:

<input type='hidden' name='id[0]' value='1'>
<input type='text' name='firstName[0]' value='John'>
<input type='checkbox' name="married[0]" value='1' checked>

… and then 1 for the next batch, 2 for the one after, and so on.

However, you probably want to deal with the resulting data by getting all three related fields at the same time. Instead of putting the data in three different arrays, put them in an array of associative arrays:

<input type='hidden' name='person[0][id]' value='1'>
<input type='text' name='person[0][firstName]' value='John'>
<input type='checkbox' name="person[0][married]" value='1' checked>

The other approach is to give the checkboxes unique values (e.g. to match the id[] input) and to then use the value instead of the position in the array. The above solution is likely easier though.

this is the normal behavior of the checkbox to not be submitted if they are unchecked.

<form method="post" action="/index.php"  enctype="multipart/form-data">
    <input type="hidden" name="id[1]" value="1">
    <input type='text' name='firstName[1]' value='John'>
    <input type='checkbox' name="married[1]" value="Y" >
    <br/>
    <input type="hidden" name="id[2]" value="2">
    <input type='text' name='firstName[2]' value='Linda'>
    <input type='checkbox' name="married[2]" value="Y" checked>
    <br/>
    <input type='hidden' name='id[3]' value='3'>
    <input type='text' name='firstName[3]' value='Mercedes'>
    <input type='checkbox' name="married[3]" value="Y" >
    <br/>
    <input type='submit' name='submit' value='update'>
</form>

will output the following

Array
(
    [id] => Array
        (
            [1] => 1
            [2] => 2
            [3] => 3
        )

    [firstName] => Array
        (
            [1] => John
            [2] => Linda
            [3] => Mercedes
        )

    [married] => Array
        (
            [2] => Y
        )

    [submit] => update
)

but if you like the married array to be filled with Y or N you can try this.

<html>
<?php
foreach ($_POST['id'] as $val){
    if(isset($_POST['married'][$val])){
        $_POST['married'][$val]="Y";
    }else{
        $_POST['married'][$val]="N";
    }
}
?>
<form method="post" action="/index.php"  enctype="multipart/form-data">
    <input type="hidden" name="id[1]" value="1">
    <input type='text' name='firstName[1]' value='John'>
    <input type='checkbox' name="married[1]" value="1" <?=($_POST['married'][1]=="Y")?" checked ":"";?>>
    <br/>
    <input type="hidden" name="id[2]" value="2">
    <input type='text' name='firstName[2]' value='Linda'>
    <input type='checkbox' name="married[2]" value="1" <?=($_POST['married'][2]=="Y")?" checked ":"";?>>
    <br/>
    <input type='hidden' name='id[3]' value='3'>
    <input type='text' name='firstName[3]' value='Mercedes'>
    <input type='checkbox' name="married[3]" value="1" <?=($_POST['married'][3]=="Y")?" checked ":"";?>>
    <br/>
    <input type='submit' name='submit' value='update'>
</form>

<pre><?php
print_r($_POST);
?></pre>
</html>

Array
(
    [id] => Array
        (
            [1] => 1
            [2] => 2
            [3] => 3
        )

    [firstName] => Array
        (
            [1] => John
            [2] => Linda
            [3] => Mercedes
        )

    [married] => Array
        (
            [1] => Y
            [2] => N
            [3] => N
        )

    [submit] => update
)