View Full Version : Checkboxes

02-12-2006, 10:20 AM
Hi, I generate a form page from my MySQL database.
let's say the table structure is:

Table A
> ID, title, active

the number of rows generated by the query is dynamic. If a field's value is 1, then the form output appears as

<input type=checkbox name=$ID checked> $title

and if not

<input type=checkbox name=$ID> $title

At the bottom of the form there is a submit button. The visitor can check/uncheck rows and then submit the form.

I need to write a loop (possibly foreach) that takes the values and processes them and writes the changes to the database.

What should I do?

02-12-2006, 12:11 PM
Set the name of the checkbox to check_id[], and the value to $id

Then work with this code to make it work :P

if ($_POST['check_id']) {
foreach ($check_id as $checkid) {
//SQL queries here. $checkid is the id of the checkboxes that are selected.

02-12-2006, 11:03 PM
Zegg, thanks for your answer. However, if a checkbox is not checked, it is not being submitted. So if you uncheck a previously checked checkbox and submit it, it is not submitted. How can I check if it has changed and update the change in the database?

02-12-2006, 11:07 PM
Any form attribute in between the form tags will be sent upon submit unless you simply don't show them, similar to Zegg's code. Or you can simply not give the checkboxes id's or names so they won't be used.

02-13-2006, 12:11 AM
Element, if a checkbox is not checked, it is not submitted. Can you exactly get my point? Or would you explain your post a little?

02-13-2006, 08:30 AM
The code I posted loops through each checkbox that's checked. It doesn't matter how many times a person checks or unchecks a checkbox. It just depends on what he submits.