Page 1 of 1

Help: Get The Name Of CheckBoxes That Is Checked In A Search

Posted: Mon Dec 29, 2008 1:42 pm
by hadi_n3065
Hi

I print out rows from a database in a table like this

echo '<form action="" method="POST">';
for($i=0;$i<$num;$i++)
{
echo '<tr>';

echo '<td style="width: 300px" class="style11">
<span lang="en" class="style13"><a href="HomeDetails.php">Description</a></span></td>';

$record=mysql_result($result,$i,'Price');
echo '<td style="width: 300px" class="style15">
<span lang="en" class="style13"><strong>'.$record.'</strong></span></td>';

$record=mysql_result($result,$i,'Space');
echo '<td style="width: 300px" class="style15">
<span lang="en" class="style13"><strong>'.$record.'</strong></span></td>';

$record=mysql_result($result,$i,'City');
echo '<td style="width: 400px" class="style15">
<span lang="en" class="style13"><strong>'.$record.'</strong></span></td>';

$record=mysql_result($result,$i,'State');
echo '<td style="width: 400px" class="style15">
<span lang="en" class="style13"><strong>'.$record.'</strong></span></td>';

$record=$i+1;
echo '<td style="width: 100px" class="style15">
<span lang="en" class="style13"><strong>'.$record.'</strong></span></td>';

$record=mysql_result($result,$i,'ID');
echo '<td class="style15"style="width: 100px" class="style11">
<input type="checkbox" name=chckbx'.$record.'></td>';

echo '</tr>';

}//end of for
echo '</table></div><input type="submit" name="BtnDel" value="Delete"></form>';

Now I want to submit this form to the next page and there I want to delete all rows where the checkbox have been checked.

My problem is: How do I get the selected ones?

Let's say that row1 and row 3 is checked. Then I would like to get the value 1 and 3 so I can remove the rows in the database with this id.

On this page there will be a lot of other checkbox.(If that is possible?)

Re: Help: Get The Name Of CheckBoxes That Is Checked In A Search

Posted: Mon Dec 29, 2008 4:50 pm
by josh
According to http RFCs, if the checkbox is set a property will be set in GET or POST with the value of the input, if it is not checked the parameter will not be sent.

Re: Help: Get The Name Of CheckBoxes That Is Checked In A Search

Posted: Tue Dec 30, 2008 1:16 am
by volomike
hadi:

I think this translates into...

I have this form with a bunch of checkboxes on it. I'm having trouble when I post this form in identifying what was checked off or not.

The only thing you're doing wrong is that you're not assigning a 'value' attribute in your checkboxes, such as:

Code: Select all

<input type="checkbox" name=chckbx' . $record . ' value="' . $record . '">'
Now your $_POST will give you a set of record IDs that you can then delete. You just iterate through $_POST like so:

Code: Select all

foreach ($_POST as $sKey => $sVal) {
  if (strpos(' ' . $sKey, 'chckbox')>0) {
    $sIDToDelete = $sVal;
    DeleteMyRecord($sIDToDelete);
  }
}

Re: Help: Get The Name Of CheckBoxes That Is Checked In A Search

Posted: Tue Dec 30, 2008 5:26 am
by hadi_n3065
Hi mike
my problem solved with your code.
Thank you very much.
bYe

Re: Help: Get The Name Of CheckBoxes That Is Checked In A Search

Posted: Tue Dec 30, 2008 6:02 am
by Bill H
And for future reference, this question was in the wrong forum; should have been on the code forum. This forum has in its title,
"This forum is not for asking programming related questions."