RLJ Posted February 17, 2011 Share Posted February 17, 2011 Hi all, I have the following code that generates a table of results from a MySQL query: $i=1; while($arr = mysql_fetch_array($result, MYSQL_NUM)) { $table .= "<tr>" ."<td width='5px';><input type='checkbox' name='transcheck".$i."'></td>" ."<td id='parent".$i."A'>".$arr[0]." ".$arr[1]."</td>" ."<td id='parent".$i."B'>".$arr[2]."</td></tr>" ."<tr style='display:none'><input type='hidden'; name='transemail".$i."'; value='".$arr[9]."'></tr>"; $i++; } $table .= "</table>"; echo $table; As you can see it generates a table containing (amongst other things): checkboxes: transcheck1,2........9...etc. hidden inputs: transemail1,2........9...etc. This table is inside a form, so that the above gets posted to another php file. What I now want to do in this 2nd php file, is to retrieve all the checkboxes and hidden inputs and then to display the values of the hidden inputs, where the corresponding checkbox has been checked. So e.g. if transcheck1, transcheck3 and transcheck12 have been checked, then I want to display transemail1, transemail3 and transemail12. I can see that this should be relatively straightforward, but I'm fairly new to this stuff, could someone pls help me out? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/228027-for-loop-to-retrieve-form-data/ Share on other sites More sharing options...
DavidAM Posted February 17, 2011 Share Posted February 17, 2011 I would define the checkboxes and emails as arrays using $i as the key: ."<td width='5px';><input type='checkbox' name='transcheck[".$i."]'></td>" ."<td id='parent".$i."A'>".$arr[0]." ".$arr[1]."</td>" ."<td id='parent".$i."B'>".$arr[2]."</td></tr>" ."<tr style='display:none'><input type='hidden'; name='transemail[".$i."]'; value='".$arr[9]."'></tr>"; then you can use a simple foreach loop: foreach ($_POST['transcheck'] as $key => $value) { /* We don't have to check the $value since checkboxes are only in POST if the user actually checked them */ echo $_POST['transemail'][$key]; } of course, you probably should not be putting email addresses on the form. Hidden fields are only hidden on the rendered page. If you view the source, you will see them. Bots love this stuff. It also leaves you open to XSS injection. You should really be using the unique ID from the database and then retrieve the email addresses from the database in the destination script. Quote Link to comment https://forums.phpfreaks.com/topic/228027-for-loop-to-retrieve-form-data/#findComment-1175855 Share on other sites More sharing options...
Psycho Posted February 17, 2011 Share Posted February 17, 2011 First, why are you making this hard on yourself (and us). Don't use the numeric index of the database results - use the field name. It makes it so much easier to understand the code. Plus, why would you create a new table row for a hidden input??? Also, you don't need the hidden fields. You just need to create the checkboxes correctly. 1. Create the checkbox names as an array 2. Set the value of the checkbox as the value you are currently assigning to the hidden input. Only the checked checkboxes and their values will be posted to the processing page. So, you can simply iterrate through all of the POSTED values from the checkboxes using the array you created in the checkbox names Form page: $i = 0; while($arr = mysql_fetch_array($result, MYSQL_NUM)) { $table .= "<tr>\n"; . " <td width='5px';><input type='checkbox' name='transcheck[]' value='{$arr[9]}'></td>\n" . " <td id='parent{$i}A'>{$arr[0]} {$arr[1]}</td>\n" . " <td id='parent{$i}B'>{$arr[2]}</td>\n" . "</tr>\n" } $table .= "</table>"; echo $table; Processing page: $checkedList = ''; foreach($_POST['transcheck'] as $value) { $checkedList .= "<li>{$value}</li>\n"; } echo "The following values were checked:<br>\n"; echo "<ul>{$checkedList}</ul>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/228027-for-loop-to-retrieve-form-data/#findComment-1175860 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.