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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.