0

my code is to checkall after I check the most upper part checkbox and then delete all checked checkboxes when I clink delete all button

my problem is I cant delete the checked boxes

I want to delete the checked rows

here is my view page

 jQuery(".checkall").on("click",function(){
                var checkall = jQuery(this);
                jQuery(".rows input[type='checkbox']").each(function(){
                    var rows_this = jQuery(this);
                    var row_id = rows_this.attr("data-id");

                    var parent = rows_this.closest("tr");
                    var view_button = parent.find(".view");
                    var href = "personal_information.php?id=" +row_id;
                    var delete_button = parent.find(".delete");
                    var href1 = "delete.php?id=" +row_id;

                        if(checkall.is(":checked")){
                            rows_this.prop("checked",true);
                            view_button.removeClass("color");
                            view_button.attr("href",href);
                            delete_button.attr("href",href1);
                            delete_button.removeClass("gray");
                        }else{
                            rows_this.prop("checked",false);
                            view_button.addClass("color");
                            view_button.attr("href","javascript:void(0);");
                            delete_button.attr("href","javascript:void(0);");
                            delete_button.addClass("gray");
                        }
                    });
                }); 

and here is my jQuery

<table><form>
    <tr>
        <th><input type="checkbox" class="checkall"></th>
        <th style="width:100px;"><center>Last Name</center></th>
        <th style="width:100px;"><center>First Name</center></th>
        <th style="width:auto;"><center>Email</center></th>
        <th style="width:100px;"><center>Birthday</center></th>
        <th style="width:auto;"><center>Action</center></th>
    </tr>
    <tr>
    <?php
    while($row = mysql_fetch_array($result))
    {
        echo "<tr class='rows' id='".$row['id']."'>";
        echo "<td>";
        echo "<center><input type='checkbox' data-id='".$row['id']."'></center>"; // data-id the id per row
        echo "<td>".$row['lastname']."</td>";
        echo "<td>".$row['firstname']."</td>";
        echo "<td>".$row['email']."</td>";
        echo "<td>".$row['bdate']."</td>";
        echo "<td><center>";
        echo "<a href='javascript:void(0);' style='text-decoration:none' class='btn delete gray'>DELETE</a> ";
        echo "<a href='javascript:void(0);' style='text-decoration:none' class='btn view color'>VIEW</a> ";
        echo "</td></center></td>";
        echo "</tr>";

    }
    ?>
    </tr>
    <tr><td>
    <input type="submit" value="Delete" class="deleteall">
    </td></tr>
</form></table>

this is my delete page php

  <?php
include 'dbconfig.php';

$id = $_GET['id'];

$sql = "DELETE FROM user WHERE id =".$id;
mysql_query($sql);
Header("Location: user.php");
?>
reyjoel
  • 29
  • 9
  • Is it any error displaying ? – dod29 Jul 18 '16 at 08:18
  • @dod29 nothing error displaying thats make it hard – reyjoel Jul 18 '16 at 08:18
  • @dod29 it just doesnt delete all the box that checked – reyjoel Jul 18 '16 at 08:21
  • do block you code by `if` statement i.e `if(isset($_GET))` in `delete.php` and `print_r($_GET)` – dod29 Jul 18 '16 at 08:22
  • You need to send array of ids back to server to delete them but seems you have not done anything about it. – Jai Jul 18 '16 at 08:22
  • In FORM you did not mention the `acton` attribute . So your page will just reload – Amar Singh Jul 18 '16 at 08:24
  • http://stackoverflow.com/a/4689190/1059101 this could help you. – Jai Jul 18 '16 at 08:24
  • @YoYo how about the script? is it correct? i think there is missing i just cant pinpoint it – reyjoel Jul 18 '16 at 08:45
  • Please check this. http://stackoverflow.com/questions/32987583/how-to-remove-checked-checkbox-if-all-sub-check-boxes-are-unchecked http://stackoverflow.com/questions/18878918/jquery-checkbox-check-all-and-remove-all – Jitendra Y Jul 18 '16 at 09:03
  • Please check this. http://stackoverflow.com/questions/32987583/how-to-remove-checked-checkbox-if-all-sub-check-boxes-are-unchecked http://stackoverflow.com/questions/18878918/jquery-checkbox-check-all-and-remove-all – Jitendra Y Jul 18 '16 at 09:04

2 Answers2

1

You need to have a name attribute to send data to backend:

echo "<center><input type='checkbox' name='id[]' data-id='".$row['id']."'></center>";
// and BTW `<center>` tag is removed in HTML5, so better to look for other way

and add a name attribute on deleteall button:

<input type="submit" name='deleteall' value="Delete" class="deleteall">

Now you can add some attributes on the form itself:

<form action='delete.php' method='get'>

and as you are sending array of checked values:

if(isset($_GET['deleteall'])){ // check if form is submitted using deleteall button
    $id = $_GET['id']; // get the array
    for($i=0;$i<count($id);$i++){ // loop through each ids in the array

        $id = $id[$i]; /// get the id in the iteration
        $sql = "DELETE FROM user WHERE id=".$id; // make the query
        $result = mysqli_query($sql); // execute to delete

    }

    Header("Location: user.php"); // when all deleted then navigate

}

Well as per your comment you can use this with jQuery to send all the checked checkboxes back to server:

$('.deleteall').click(function(e){
    e.preventDefault();  // <---use this stop the form submission.

    var arr2send = $('.rows input[type=checkbox]:checked').map(function(){
        return $(this).data('id');
    }).get();

    var deleteallurl = 'delete.php?id='+JSON.stringify(arr2send);

    window.location.href = deleteallurl;

});

Yet you need a loop at the backend to iterate over all the ids in the array and delete them and after all the deletion you can navigate back to user.php page.

Jai
  • 74,255
  • 12
  • 74
  • 103
0

SQL Injection Alert!!

Even if your code isn't working:

$sql = "DELETE FROM user WHERE id =".$id;

is a BAD PRACTICE!!

You're asking for SQL Injections and that is something you don't want! Even script kiddies can drop your database, etc.

Please check: https://www.owasp.org/index.php/SQL_Injection, at least for your own sake as a developer!


Besides that: Always put an exit after Header("Location: ...)

Check http://php.net/manual/en/function.header.php

321X
  • 3,153
  • 2
  • 30
  • 42