This is a discussion on Handle Multiple Check Boxes? within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I have a PHP generated page which displays X many records. Each record has a checkbox preceding it. The user ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I have a PHP generated page which displays X many records. Each record has
a checkbox preceding it. The user checks several checkboxes, and hits a delete button. All the corresponding records will be deleted. But I'm running into difficulty... Right now the NAME property of each check box is the primary key of the corresponding record. Hence if I know which checkboxes are checked, I simply use DELETE using the NAME value. Generally speaking, how do I get the server side to see which check boxes were checked? The check box names may not be sequential, if any records have been deleted previously, and the first check box might be a number greater than 0. Is there an easy mechanism to do this? Some kind of built in cnotrol array allowing me to loop over every check box that was on the form submitted? I could store the last and first checkbox number in a hidden input, then loop starting/ending at those values, but that may loop over a lot of controls that do not exist. Thoughts? <Ade -- Adrian Parker. Ordained priest. <adrian.parker@sympatico.ca> "A society that views graphic violence as entertainment ...should not be surprised when senseless violence shatters the dreams of it's youngest and brightest..." - Ensign (March 2004) |
|
|||
|
Adrian Parker wrote:
> I have a PHP generated page which displays X many records. Each record has > a checkbox preceding it. The user checks several checkboxes, and hits a > delete button. All the corresponding records will be deleted. > > But I'm running into difficulty... > > > Right now the NAME property of each check box is the primary key of the > corresponding record. Hence if I know which checkboxes are checked, I > simply use DELETE using the NAME value. > > Generally speaking, how do I get the server side to see which check boxes > were checked? > > The check box names may not be sequential, if any records have been deleted > previously, and the first check box might be a number greater than 0. > > Is there an easy mechanism to do this? Some kind of built in cnotrol array > allowing me to loop over every check box that was on the form submitted? > > I could store the last and first checkbox number in a hidden input, then > loop starting/ending at those values, but that may loop over a lot of > controls that do not exist. > > > Thoughts? > > > > <Ade generate the list with something like this: $result = mysql_query("SELECT * FROM table", $fh); while ($row = mysql_fetch_array($result)) { echo "<input name="id[]" type="check value="$row[0]"><br>"; } then when it gets passed to the serverside, $_POST['id'] will be an array containing the values of the checked boxes. Just walk through the array deleting them one by one. Off the top if my head without testing: $delete = $_POST['id']; foreach ($delete as $this) { mysql_query("delete from table where id=$this"); } I hope this helps, and btw, it took me a while to understand how checkboxes were handled too. |
|
|||
|
Milambar wrote:
> Adrian Parker wrote: > >> I have a PHP generated page which displays X many records. Each >> record has >> a checkbox preceding it. The user checks several checkboxes, and hits a >> delete button. All the corresponding records will be deleted. >> >> But I'm running into difficulty... >> >> >> Right now the NAME property of each check box is the primary key of the >> corresponding record. Hence if I know which checkboxes are checked, I >> simply use DELETE using the NAME value. >> >> Generally speaking, how do I get the server side to see which check boxes >> were checked? >> >> The check box names may not be sequential, if any records have been >> deleted >> previously, and the first check box might be a number greater than 0. >> >> Is there an easy mechanism to do this? Some kind of built in cnotrol >> array >> allowing me to loop over every check box that was on the form submitted? >> >> I could store the last and first checkbox number in a hidden input, then >> loop starting/ending at those values, but that may loop over a lot of >> controls that do not exist. >> >> >> Thoughts? >> >> >> >> <Ade > > generate the list with something like this: > > $result = mysql_query("SELECT * FROM table", $fh); > while ($row = mysql_fetch_array($result)) { > echo "<input name="id[]" type="check value="$row[0]"><br>"; > } > > then when it gets passed to the serverside, $_POST['id'] will be an > array containing the values of the checked boxes. Just walk through the > array deleting them one by one. > > Off the top if my head without testing: > > $delete = $_POST['id']; > foreach ($delete as $this) { > mysql_query("delete from table where id=$this"); > } > > I hope this helps, and btw, it took me a while to understand how > checkboxes were handled too. Meh, dont forget to escape the quotes in the generation part: echo "<input name=\"id[]\" type=\"checkbox\" value=\"$row[0]\"><br>"; Sorry. But it is 3am here at the moment. |