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);
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
?>

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.