I'm trying to select all checkboxes from my table (check all/uncheck all function) but can't seem to get it right as all solutions I've seen use a form and separate input checkboxes but my code is called from within a function to echo the results of what is stored in my database table. I need the user to be able to check the first checkbox (which currently does nothing) and thus result in all other checkboxes being selected. Please can someone assist me in how I can get this to work, here is the code I've been trying to implement using php and jquery:
function BuildPersonResultTable($result){
$personTable= "<table id='main-db' border='1'>";
$personTable= $personTable . "<tr>";
$personTable= $personTable . "<th><input type='checkbox' class='chk_boxes' /></th>";
$personTable= $personTable . "<th>ID</th>";
$personTable= $personTable . "<th>First Name</th>";`
$personTable= $personTable . "<th>Surname</th>";
$personTable= $personTable . "<th>E-mail</th>";
$personTable= $personTable . "<th>Phone Number</th>";
$personTable= $personTable . "<th>Parent</th>";
$personTable= $personTable . "<th>Volunteer</th>";
$personTable= $personTable . "<th>Business Contact</th>";
$personTable= $personTable . "<th></th>";
$personTable= $personTable . "<th></th>";
$personTable= $personTable . "</tr>";
while($row = mysqli_fetch_array($result))
{
$personTable= $personTable . BuildPersonResultRow($row);
}
$personTable= $personTable . "</table>";
return $personTable;
}
function BuildPersonResultRow($row){
$personTableHtml = "<tr>";
$personTableHtml = $personTableHtml . "<td width='60px' id='chooseSender'>";
$personTableHtml = $personTableHtml . "<input type='checkbox' class='chk_boxes1' name='id_list[]' value='" . $row['id'] ."'/>";
$personTableHtml = $personTableHtml . "</td>";
$personTableHtml = $personTableHtml . "<td>" . $row['id'] . "</td>";
$personTableHtml = $personTableHtml . "<td>" . $row['name'] . "</td>";
$personTableHtml = $personTableHtml . "<td>" . $row['surname'] . "</td>";
$personTableHtml = $personTableHtml . "<td>" . $row['email'] . "</td>";
$personTableHtml = $personTableHtml . "<td width='70'>" . $row['alt_contact'] . "</td>";
$personTableHtml = $personTableHtml . "<td>" . boolToYesNo($row['parent']) . "</td>";
$personTableHtml = $personTableHtml . "<td>" . boolToYesNo($row['volunteer']) . "</td>";
$personTableHtml = $personTableHtml . "<td>" . boolToYesNo($row['business']) . "</td>";
$personTableHtml = $personTableHtml . '<td><a class= "side-btns" href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
$personTableHtml = $personTableHtml . '<td><a class= "side-btns" href="delete.php?id=' . $row['id'] . '">Remove</a></td>';
$personTableHtml = $personTableHtml . "</tr>";
return $personTableHtml;
};
And the HTML code:
<html>
<head>
<link rel="stylesheet" type="text/css" href="CSS/DbCSS.css">
<script type="text/javascript" src="jquery-1.6.4.min.js">
$('.chk_boxes').click(function(){
var chk = $(this).attr('checked')?true:false;
$('.chk_boxes1').attr('checked',chk);
});
</script>
</head>