April 24, 2016
JavaScript Onclick Confirm on Gridview Delete – PHP & MySQLi

Hi friends, in this tutorial I show you JavaScript Onclick confirmation on gridview delete in PHP & MySQLi. Delete is one of the fundamental processes performed on the database. It’s always a good habit to confirm the delete action of users with an alert message to avoid accidental clicks and data deletion. In this, we’ll see how to delete row(s) from MySQL database with confirmation alert message in PHP.
disp.php
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 |
<?php //Connection for database include 'conn.php'; //Select Database $sql = "SELECT * FROM data"; $result = $conn->query($sql); ?> <!doctype html> <html> <body> <h1 align="center">Employee Details</h1> <table border="1" align="center" style="line-height:25px;"> <tr> <th>Employee ID</th> <th>Name</th> <th>Gender</th> <th>Department</th> <th>Address</th> <th>Mobile Number</th> <th>Email</th> <th>Edit</th> <th>Delete</th> </tr> <?php //Fetch Data form database if($result->num_rows > 0){ while($row = $result->fetch_assoc()){ ?> <tr> <td><?php echo $row['empid']; ?></td> <td><?php echo $row['name']; ?></td> <td><?php echo $row['gender']; ?></td> <td><?php echo $row['department']; ?></td> <td><?php echo $row['address']; ?></td> <td><?php echo $row['mobile']; ?></td> <td><?php echo $row['email']; ?></td> <!--Edit option --> <td><a href="edit.php?edit_id=<?php echo $row['empid']; ?>" alt="edit" >Edit</a></td> <!-- Delete Buttion --> <td><input type="button" onClick="deleteme(<?php echo $row['empid']; ?>)" name="Delete" value="Delete"></td> </tr> <!-- Javascript function for deleting data --> <script language="javascript"> function deleteme(delid) { if(confirm("Do you want Delete!")){ window.location.href='delete.php?del_id=' +delid+''; return true; } } </script> <?php } } else { ?> <tr> <th colspan="2">There's No data found!!!</th> </tr> <?php } ?> </table> </body> </html> |
delete.php
1 2 3 4 5 6 |
<?php include_once("conn.php"); $select = "DELETE from data where empid='".$_GET['del_id']."'"; $query = mysqli_query($conn, $select) or die($select); header ("Location: disp.php"); ?> |
Download
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]