Level: PHP Learner
I’m stuck with a checkbox problem. I have a db that contains names and unique id numbers.
Using a query, I am pulling a selection of students and showing them to a user in an ultra simple HTML table on a form. Each row begins with a checkbox. The method is POST. So far, so good. My table looks like this:
+-----------+----------+----------+
| SELECT | NAME | ID |
+-----------+----------+----------+
| [] | John | 2233 |
+-----------+----------+----------+
| [] | Susie | 5577 |
+-----------+----------+----------+
[-SUBMIT-]
My problem is that I cannot seem to make the checkbox associate with each record’s unique ID. Once the user has selected rows and clicked submit, the $_POST array remains empty.
None of my beginners books reference this specific issue. They go through the “regular” checkbox routines that don’t involve interacting with rows from a db. I also could not find an issue on Stackoverflow that addresses this. Also tried Google: plenty of stuff on checkboxes, but I couldn’t find any that helped me on this problem.
I like to do it this way:
Number the checkboxes sequentially (1 to 100) and add a hidden field connecting the row number to a real database ID:
Store the total number of rows in another hidden field
<input type='hidden' name='row_total' value='99'>Then, in the receiving script, iterate from 1 to the total number of rows using
for, check whether this row was selected, and get the associated database ID:the latter, of course, needs to be properly sanitized and escaped in case it is processed further.