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.

2
  • you can try using type="radio" Commented Apr 11, 2017 at 7:46
  • @MancharyManchaary And how it can help? Commented Apr 11, 2017 at 7:51

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.