-1

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?

  • @PaulCrovella It's same case for one unchecked chechbox, but we can resolve that by adding hidden input with the same name of chechbox, with value='0'. So checkbox value will be received as 0 for unchecked, and 1 for checked. – Amine Dahou Jul 15 '18 at 21:51
  • @AmineDahou — That only works when you don't have multiple checkboxes with the same name / the name doesn't cause PHP to generate an array. – Quentin Jul 15 '18 at 21:56
  • @AmineDahou — It looks like you failed to change `draft` to `married[]` a couple of times in your example code. – Quentin Jul 15 '18 at 22:00
  • @Quentin corrected. – Amine Dahou Jul 15 '18 at 22:07
  • @AmineDahou I suggest rewriting your question to be clearer and more specific, at the moment it's proving hard to understand what exactly is the problem you're referring to : ) –  Jul 15 '18 at 22:09

2 Answers2

1

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I used the array index as mentionned, combined with hidden html input field with sames name and same index, but value = "0" – Amine Dahou Jul 15 '18 at 22:35
0

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
)