delete a multiple selected rows in PHP without Ajax
In this post, we have shared How To delete multiple selected rows in PHP without Ajax. normally we have added the button or anchor tag for delete single data. but when we have large data and we will delete the single data, at that time it will take more time. Then at that time, this functionality will helpful.
For discussion, How To delete multiple selected rows in PHP without Ajax. after then configuration database and fetch the data and also we will have set bootstrap and take the form and table. here pass data into TR and TD tag through ‘WHILE’ loop and also take checkbox with multiple names in every single row.
When we will have checked the checkbox and then after click on the delete button then the form will be submitted. it will count the number of checkboxes that are checked. it will get the checkbox value using the for a loop after then delete data one by one.
See below an example of How To delete multiple selected rows in PHP without Ajax.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | <?php $hostname="localhost"; $username="root"; $password=""; $database="php"; $conn = mysqli_connect($hostname,$username,$password,$database); if(isset($_POST['btnDelete'])) { $checkbox = $_POST['check']; for($i=0;$i<count($checkbox);$i++) { $del_id = $checkbox[$i]; mysqli_query($conn,"delete from users where id='".$del_id."'"); $message = "Data Successfully Deleted"; } } $result = mysqli_query($conn,"select * from users"); ?> <!DOCTYPE html> <html> <head> <title>How To delete a multiple selected rows in PHP without Ajax</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="row"> <div class="col-lg-12"> <?php if(isset($message)) { ?> <div class="alert alert-success alert-dismissible"> <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a> <strong>Success!</strong> <?php echo $message; ?> </strong> </div> <?php } ?> </div> </div> <form method="post" action=""> <div class="row"> <div class="col-lg-12"> <button type="submit" class="btn btn-danger btn-sm" name="btnDelete">DELETE</button> </div> </div> <br/> <table class="table table-bordered"> <thead> <tr> <th><input type="checkbox" id="checkAll"> <label for="checkAll">Select All</label></th> <th>Id</th> <th>First Name</th> <th>Last Name</th> <th>Email</th> <th>Created Date</th> </tr> </thead> <?php $i=0; while($row = mysqli_fetch_array($result)) { ?> <tr> <td><input type="checkbox" id="checkItem" name="check[]" value="<?php echo $row["id"]; ?>"></td> <td><?php echo $row["id"]; ?></td> <td><?php echo $row["first_name"]; ?></td> <td><?php echo $row["last_name"]; ?></td> <td><?php echo $row["email"]; ?></td> <td><?php echo $row["created"]; ?></td> </tr> <?php $i++; } ?> </table> </form> </div> <script> $("#checkAll").click(function () { $('input:checkbox').not(this).prop('checked', this.checked); }); </script> </body> </html> |
I have shared demo Button or URL. So you can click on Button or URL and show the demo.