Jump to content

Buttons dont work


Russia

Recommended Posts

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

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