![]() |
Checkboxes and db rows
Hello again.
I have a query that pulls rows from a mysql db. The data is displayed in a simple HTML table. The first column of the HTML table has a checkbox for each row. It looks a little like this: PHP Code:
The user is allowed to select rows, each of which has a unique ID. I have spent untold hours trying to understand how to know which rows (i.e., the unique ids) were selected by checkboxes. How would I go about this? The HTML table is on a form with a POST method. So, for example, if a user checks the checkbox on John's row, how can I retrieve John's ID into a $_POST array? None of my books go into this specifically. They only give checkbox examples such as What is your fav. ice cream? Vanilla, Chocolate, blah, blah. Thanks! Dave Note: the above table is, of course, simplified. The HTML table actually contains about a dozen fields and up to a couple of hundred records resulting fromt the db query. |
You are looking at something like....
> # jump out of php code <INPUT TYPE=CHECKBOX NAME=" <? $id > "> <? rest of php code can't off the top of my head remember if the quotes are required or not, but basically that will create a checkbox and name it the id of the current row pulled from the db... So you need to either jump out of your php code, similar to above, or just echo the html code... Not sure which is best, or most efficient, but either method will work... |
Thanks, maeltar...
Yes, I have all that set up and I have tried many unsuccessful variations. I really need to know what goes into the attributes of the <INPUT...>, and also how to process this at the end of the script. PHP Code:
Dave |
To be honest I would not code it the way you have done, I would be (maybe wrongly) do it something like this...
Code:
|
Thanks again. I've reworked my script and have posted it below. The problem is that after some rows are selected by checkboxes and the submit button is clicked, the $_POST array is empty, using print_r($_POST) to check.
Any suggestions? -- Dave PHP Code:
|
Have a look and see if you are getting any data at all...
print_r($GLOBALS) |
Will do. Also, I'm going to go through this thing again with a fine-tooth comb and will hopefully find something. It is interesting how this little thing has given me so much trouble!
|
For the line where you output the checkbox you need to make the following changes
PHP Code:
You also did not properly close the <input> tag as you missed out the closing /> When the form is submitted now you should be able to do a var_dump($_POST['checkbox']). If it is empty it's because you didn't check any of the boxes. So try checking a few boxes and then it should give you an array with the IDs of those records that you selected. |
| All times are GMT. The time now is 04:46 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0