Russia Posted September 24, 2009 Share Posted September 24, 2009 I have a script to select all the checkboxes and a button to deselect all of them. It isnt working. Both versions arent working. VERSION 1 OF THE SCRIPT! <script type="text/javascript"> function allDelcheck(tf) { var buttons = document.myform.elements("delcheck[]"); // this works! for ( var b = 0; b < buttons.length; ++b ) { buttons[b].checked = tf; } } </script> <?php require("inc/config.php"); if (isset($_POST['del'])) { for ($count = 0;$count<count($_POST[delchk]);$count++) { $delete = $_POST[delchk][$count]; $query = "DELETE FROM persons WHERE id = '$delete'"; $result = mysql_query($query); if (!$result) { die("Error deleting persons! Query: $query<br />Error: ".mysql_error()); } } } $result = mysql_query("SELECT * FROM persons"); // Check how many rows it found if(mysql_num_rows($result) > 0){ echo "<table class=\"gridtable\"> <thead> <tr> <th align=\"center\" scope=\"col\">Delete?</th> <th align=\"center\" scope=\"col\">First Name</th> <th align=\"center\" scope=\"col\">Last Name</th> <th align=\"center\" scope=\"col\">Highscores</th> <th align=\"center\" scope=\"col\">Date</th> <th align=\"center\" scope=\"col\">IP Address</th> </tr> </thead> <tbody>"; echo "<form name = 'myform' action='' method='post'>"; while($row = mysql_fetch_array($result)) { echo "<tr align=\"center\">"; echo '<td><input type="checkbox" id="delchk" name="delcheck[]" value="'.$row['id'].'" /></td>'; echo "<td class=\"valid\" >" . $row['FirstName'] . "</td>"; echo "<td class=\"valid\" >" . $row['LastName'] . "</td>"; echo "<td><a target=frame2 href='" ."profile.php?user=". $row['FirstName'] ."'>Check Highscores</a></td>"; echo "<td>" . $row['AddedDate'] . "</td>"; echo "<td>" . $row['Ip'] . "</td>"; echo "</tr>"; } echo "</tbody>"; echo "</table>"; echo "<hr>"; echo "<input type='submit' name = 'del' value='Delete Selected'></form>"; echo "<input type='button' onclick='allDelcheck(true);' value='Select All'>"; echo "<input type='button' onclick='allDelcheck(false);' value='UnSelect All'>"; } else{ // No rows were found ... echo '<center>No logged accounts.</center>'; } mysql_close(); ?> ---- VERSION 2 OF THE SCRIPT! <script type="text/javascript"> function checkall(delchk) { for (i = 0; i < delchk.length; i++) delchk[i].checked = true; } </script> <script type="text/javascript"> function uncheckall(delchk) { for (i = 0; i < delchk.length; i++) delchk[i].checked = false; } </script> PHP <?php error_reporting(E_ALL); require("inc/config.php"); if (isset($_POST['del'])) { for ($count = 0;$count<count($_POST[delchk]);$count++) { $delete = $_POST[delchk][$count]; $query = "DELETE FROM persons WHERE id = '$delete'"; $result = mysql_query($query); if (!$result) { die("Error deleting persons! Query: $query<br />Error: ".mysql_error()); } } } $result = mysql_query("SELECT * FROM persons"); // Check how many rows it found if(mysql_num_rows($result) > 0){ echo "<table id=\"mytable\"> <thead> <tr> <th align=\"center\" scope=\"col\">Delete?</th> <th align=\"center\" scope=\"col\">First Name</th> <th align=\"center\" scope=\"col\">Last Name</th> <th align=\"center\" scope=\"col\">Profile</th> <th align=\"center\" scope=\"col\">Date</th> <th align=\"center\" scope=\"col\">IP Address</th> </tr> </thead> <tbody>"; echo "<form name = 'myform' action='' method='post'>"; while($row = mysql_fetch_array($result)) { echo "<tr align=\"center\">"; echo '<td><input type="checkbox" id="delchk" name="delchk[]" value="'.$row['id'].'" /></td>'; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "<td><a target=frame2 href='" ."profile.php?user=". $row['FirstName'] ."'>Check Profile</a></td>"; echo "<td>" . $row['AddedDate'] . "</td>"; echo "<td>" . $row['Ip'] . "</td>"; echo "</tr>"; } echo "</tbody>"; echo "</table>"; echo "<hr>"; echo "<input type='submit' name = 'del' value='Delete Selected'></form>"; echo "<input type='button' onclick='checkall(document.myform[\"delchk[]\"]);' value='Select All'>"; echo "<input type='button' onclick='uncheckall(document.myform[\"delchk[]\"]);' value='UnSelect All'>"; } else{ // No rows were found ... echo 'No registered members.'; } mysql_close(); ?> Link to comment https://forums.phpfreaks.com/topic/175411-buttons-dont-work/ Share on other sites More sharing options...
JasonLewis Posted September 25, 2009 Share Posted September 25, 2009 Try something like this: <script type="text/javascript"> function checkAll(d){ var el = document.getElementsByTagName("input"); for(var i = 0; i < el.length; i++){ if(el[i].type == "checkbox"){ el[i].checked = d; } } } </script> Then your PHP part: echo "<input type='button' onclick='javascript: checkAll(true);' value='Select All'>"; echo "<input type='button' onclick='javascript: checkAll(false);' value='UnSelect All'>"; Link to comment https://forums.phpfreaks.com/topic/175411-buttons-dont-work/#findComment-924706 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.