1

I am struggling to use the form below to post and process to my php script. Currently it only brings up those which are selected. I need it to pass the value of 0 where a checkbox isn't selected. I then need my php to iterate through all rows and update by sql.

<form action="update.php" method="post">
    <table class="table table-striped">
        <tr class="name_5">
        <input type="hidden" name="id[]" value="5" />
            <td>
                iD: 5 Name: Sam
            </td>
            <td>
                <input type="checkbox" name="block1[]"  value="1" />
            </td>
            <td>
                <input type="checkbox" name="block2[]"  value="1" />
            </td>
            <td>
                <input type="checkbox" name="block3[]"  value="1" />
            </td>
            <td>
                <textarea name="notes[]" cols="20" rows="1"></textarea>
            </td>
        </tr>
        <tr class="name_4">
        <input type="hidden" name="id[]" value="4" />
            <td>
                ID: 4 Name: Joanne<br/>
            </td>
            <td>
                <input type="checkbox" name="block1[]"  value="1" />
            </td>
            <td>
                <input type="checkbox" name="block2[]"  value="1" />
            </td>
            <td>
                <input type="checkbox" name="block3[]"  value="1" />
            </td>
            <td>
                <textarea name="notes[]" cols="20" rows="1"></textarea>
            </td>
        </tr>
        <tr class="name_2">
        <input type="hidden" name="id[]" value="2" />
            <td>
                ID: 2 Name: Fiona<br/>
            </td>
            <td>
                <input type="checkbox" name="block1[]" checked value="1" />
            </td>
            <td>
                <input type="checkbox" name="block2[]" checked value="1" />
            </td>
            <td>
                <input type="checkbox" name="block3[]"  value="1" />
            </td>
            <td>
                <textarea name="notes[]" cols="20" rows="1"></textarea>
            </td>
        </tr>
    </table>
</form>

I've tried using hidden fields using value of 0 for each row but this didn't work, plus I would rather handle it more elegantly.

user3189734
  • 665
  • 1
  • 9
  • 27

2 Answers2

1

When ever you are dealing with checkbox keep one thing in mind that only those checkbox which are selected are submitted with the form submit. So to send all the checkbox irrespective of checked/unchecked use Jquery to iterate over each checkbox and put a check if the checkbox is checked then push 1 in an array otherwise 0 and send that array to server and process it on server.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
1

Unchecked checkboxes must have a default value '0' or 'false' or null etc.. in the related database column.

Checkboxes are 0/1 values, so you can use ENUM type of column, with default value '0'. This is for checkbox related column on your database.

So when you don't update them, because they are not posted, they will still have their default value.

But if you still want them to be posted on form submit, you can use answer on this link Post the checkboxes that are unchecked

Community
  • 1
  • 1
Оzgur
  • 432
  • 2
  • 10