handling checked checkboxes PHP
I have a table that takes data from the database like so: (Is not a form)
if (mysql_num_rows($result)) {
echo "<table id='logs' border='1' cellspacing='0' width='62%'>";
echo "<tr>";
echo "<th width='15%'>Time Registered</th>";
echo "<th width='15%'>Username</th>";
echo "<th width='15%'>Password</th>";
echo "&l开发者_Python百科t;th width='15%'>IP Address</th>";
echo "<th width='2%'><a href=\"#\" onclick=\"checkAll(this);\">Mark</a></th>";
echo "<th width='2%'>Delete</th>";
echo "</tr>";
while ($row = mysql_fetch_row($result))
{
echo "<tr>";
echo ("<p><td>$row[2]</td><td>$row[0]</td><td>$row[1]</td><td><i>$row[3]</i></td><td><center><input type=\"checkbox\" name=\"mark[]\"/></center></td><td><a href=\"delete.php?time=$row[2]&user=$row[0]&pass=$row[1]&ip=$row[3]\"><center>[x]</center></a></td></p>");
echo "</tr>";
}
echo "</table>";
}
The part <input type=\"checkbox\" name=\"mark[]\"/>
is the checkbox. How can I find and handle the checked checkboxes?
if(mark[$checked]) {
//delete data from database if row checked
}
foreach($_REQUEST['mark'] as $value){
echo "$value was selected\n <br />";
}
if you want to know which one wasn't selected then store all possible selections into an array and walk ofer this array and do someting like
foreach($poss_select as $key=>$val){
if(!in_array($val,$_REQUEST['mark']){
$not_selected[$key] = $value;
}else{
deleteRow($value);
}
}
$_REQUEST[ 'mark' ]
will be an array of all of the checked boxes.
You would need to do some AJAX or a form submit to pass that checkbox data to PHP for processing.
But before you can do that, you would need to include a value attribute on those checkboxes. Currently, $_POST['mark'] (if you used POST to submit the form) would be a 0-based array of the checked checkboxes (and only the checked ones).
I would recommend outputting the user id as the value of checkbox to help out with identifying the marked users.
Then you could do
foreach ($_POST['mark'] as $marked) {
// $marked would contain the value attribute of the checked checkboxes
// and you could run a SQL query for each value of $marked.
}
精彩评论