Jump to content

Select All Checkboxes


Dysan

Recommended Posts

I have the following PHP code that creates a simple 2x2 table, and features check boxes.

 

Upon clicking the top (title) checkbox, how do I check/uncheck each of the records check boxes?

 

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
  die(mysql_error());
}

mysql_select_db("db", $con);

$result = mysql_query("SELECT * FROM person");

echo '<table border="1">
<tr>
<th><input type="checkbox" name="checkbox" value="checkbox"></th>
<th>First Name</th>
<th>Last Name</th>
</tr>';

while($row = mysql_fetch_array($result))
{
  echo '<tr>';
  echo '<td><input type="checkbox" name="checkbox" value="checkbox"></td>';
  echo '<td>'.$row['FirstName'].'</td>';
  echo '<td>'.$row['LastName'].'</td>';
  echo '</tr>';
}
echo '</table>';

mysql_close($con);
?>

 

Link to comment
https://forums.phpfreaks.com/topic/83396-select-all-checkboxes/
Share on other sites

Give the table and the check all button unique ids and this code should work

 

function checkAll(){
    var table_eles  = document.getElementById('table_id').getElementsByTagName('input');
    var action       = document.getElementById('check_all_id').checked ? true : false;
    foreach(var x in table_eles){
        if(table_eles[x].type == 'checkbox'){
            table_eles.checked = action;
        }
    }
}

and you can do some inline js on your input field

<input type="checkbox" name="checkbox" id="check_all_id" onclick="checkAll();" />

 

 

Link to comment
https://forums.phpfreaks.com/topic/83396-select-all-checkboxes/#findComment-424331
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.