Re: Processing a table of input fields
| From: | Todd Cary | Date: | Sat, 12 May 2007 00:41:27 +0000 |
| Subject: | Re: Processing a table of input fields | ||
| References: | 1 2 3 | Groups: | php.general php.general |
| Request: | Send a blank email to php-general+get-254832@lists.php.net to get a copy of this message | ||
Jim Lucas wrote:
Richard Davey wrote:Thanks again! Have it working for the initial entry and the recreation of the table for editing based on the array. ToddTodd Cary wrote:I would do it even a different way <tr> <td><input type="checkbox" name="list[590][attend]" value="1"></td> <td>05/11/2007</td> <td>Theressa</td> <td>Bryant</td> <td><input type="text" name="list[590][pay]" value="16" size="5" maxlength="4"></td> <td><input type="text" name="list[590][charge]" value="16" size="5" maxlength="4"></td> <td><input type="text" name="list[590][note]" size="26" maxlength="25"></td> </tr> Then on the PHP side this. if ( isset($_POST['list']) && count($_POST['list']) > 0 ) {I create a table of input fields so the user (secretary at a Rotary meeting) can check mark if the person attended and how much they paid for lunch. Each input field name has the user ID as part of it. What is the best way to process the table when the submit button is pressed? There are about 50 rows in the table. Sample of one row for member 590: <tr> <td><input type="checkbox" name="590_attend" value="1"> </td> <td>05/11/2007</td><td>Theressa</td><td>Bryant</td><td><input type="text" name="590_pay" value="16" size="5" maxlength="4"></td> <td><input type="text" name="590_charge" value="16" size="5" maxlength="4"></td> <td><input type="text" name="590_note"" size="26" maxlength="25"></td> </tr>Personally I'd do it like this (if you are displaying say 50 checkboxes on the page at once) Name each checkbox so the they go into an array, i.e.: <input type="checkbox" name="attend[]" value="590"> <input type="checkbox" name="attend[]" value="591"> <input type="checkbox" name="attend[]" value="592"> Then in your PHP script you can simply loop through the attend array ($_POST['attend']) and extract all the IDs of those people who were ticked. If they weren't ticked, they won't be in your array. Cheers, Richforeach ( $_POST['list'] AS $id => $data ) { if ( isset($data['attend']) && $data['attend'] == '1' ) { # do stuff related to them attending the meal } }} This way, everything the grouped in one big array. You don't have to worry about checking for values in each array, it is all right there. Keeping things in sync is much easier this way. Hope this helps