Jump to content

[SOLVED] Checkbox not being check on CheckAll button


Russia

Recommended Posts

I currently have this script, I need it to check when I click the checkall button.

 

Hm... now for some reason the select all button isnt working...

 

Current code:

 

<?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\">High Scores</th>
        <th align=\"center\" scope=\"col\">Date of Entry</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?user1=". $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='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"; 


echo "</tbody>";
echo "</table>";
}

mysql_close($con);
?>

I have answer that before here is the your last post that I answer that question

 

http://www.phpfreaks.com/forums/index.php/topic,266488.0.html

 

and by the way. again you forgot to make a JavaScript function named checkall and uncheckall

<script type="text/javascript">
   function checkall(chek)
   {
      for (i = 0; i < chek.length; i++)
      chek[i].checked = true;
   }
</script>

<script type="text/javascript">
   function uncheckall(chek)
   {
      for (i = 0; i < chek.length; i++)
      chek[i].checked = false;
   }
</script>

 

Soz Forgot To add.

How does this code, match your JavaScript button code, mate please explain russia.

 

<script type="text/javascript">
   function checkall(chek)
   {
      for (i = 0; i < chek.length; i++)
      chek[i].checked = true;
   }
</script>

<script type="text/javascript">
   function uncheckall(chek)
   {
      for (i = 0; i < chek.length; i++)
      chek[i].checked = false;
   }
</script>

merck_delmoro has shown you ever think needed, can you read please?

 

there also a link provided to see how it works from merck_delmoro

 

now you no why learning java script is far better then copy and pasting .

 

Something more important then that in your code, u use the ip address of the user, well it not the correct way for security issues use the user's id ....

 

example, i can set my own ip, if i am behind a proxy server, but it not mine is it?

 

never rely on users ip address ever.

there are no errors it checks all the check boxes when I click it are you sure you are clicking the right button???

 

<?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\">High Scores</th>
        <th align=\"center\" scope=\"col\">Date of Entry</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?user1=". $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='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"; 


echo "</tbody>";
echo "</table>";
}

mysql_close($con);
?>

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.