views:

139

answers:

2

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 kinda 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.

Thanks.

+6  A: 

Just do something like this:

<input type="checkbox" name="ids[]" value="2233" /> ... rest of row
<input type="checkbox" name="ids[]" value="5577" /> ... rest of row

Now, in PHP, you can get the selected ids like this:

$ids = $_POST['ids'];
if( empty($ids) ) $ids = array();

This sets $ids to an empty array if the form was submitted without any of the checkboxes checked.

Doug Neiner
Beautifully simple. +1
Pekka
|| [] is javascript, not php
stereofrog
`[]` needs to become `Array()` (or this is a PHP notation I don't know). And PHP, unlike Javascript has `or` :D
Pekka
Haha, yes, that was a special language I like to call "PHPRubyScript" :) Thanks for catching those errors.
Doug Neiner
Lol.. my answer has glaring problems, and I get no downvotes. I fix them, and it is downvoted without comment.
Doug Neiner
The mysteries of SO... :)
Pekka
Thank you, Doug. I'm afraid that the PHP portion of your suggestion is a bit over my head, but I will work with it until I understand it.
dave
Hey, no problem. It just gives you a PHP array with the id's. For instance if both were checked, it would give you the equivalent of `$ids = array(2233, 5577)`
Doug Neiner
+1  A: 

Edit: If your form doesn't get more complex as you describe, take Doug Neiner's approach, as it is way simpler. This approach is right if a table is likely to have a number of columns.

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:

<input type="checkbox" name="row_1" value="checked">`
<input type='hidden' name='row_1_id' value='2233'>`

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:

for ($i = 1; $i <= $number_of_rows; $i++)
{
  if ($_POST["row_$i"] == "checked")
   {
     $database_id_unsafe = $_POST["row_{$i}_id"];
 ...

the latter, of course, needs to be properly sanitized and escaped in case it is processed further.

Pekka
+1 Nice, you could skip the test by using this `<input type="checkbox" name="row[2223]" value="checked" /><input type="hidden" name="row[2223]" value="not_checked" />` then the value would be submitted regardless of if the checkbox was submitted. Then you just have to iterate over the returned `row` parameter using a `$key => $value` pair and you would have both the ID and the checked/unchecked state. Or am I thinking Ruby again :)
Doug Neiner
Interesting idea. I can't test it right now but sounds like it would work!
Pekka
Thanks, Pekka. Yes, the actual number of fields will potentially grow substantially. I'm going to try to implement this concept.
dave