ramiwahdan Posted April 6, 2020 Share Posted April 6, 2020 Hi, i have delete link for records and it goes to other page to delete all related records in tables. is there away to get confirmation before the delete process starts? <?Php include('session.php'); include('dbcon.php'); if(isset($_GET['Del'])) { $userid = $_GET['Del']; $query = "delete from staff where OracleID='".$userid."'"; $result = mysqli_query($con,$query); if($result) { echo "staff deleted"; } else { echo 'check the errors!'; } $query2 = "delete from attendance_records where OracleID='".$userid."'"; $result2 = mysqli_query($con,$query2); if($result2) { echo "records deleted"; } else { echo 'check the errors!'; } header("location:viewstaff.php"); } else { header("location:viewstaff.php"); } ?> how to add confirm message with php before deletion is processed? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 6, 2020 Share Posted April 6, 2020 Add a call to the JS onclick event to your submit button. Point it to a JS function that asks the user to confirm, perhaps twice even. Quote Link to comment Share on other sites More sharing options...
ramiwahdan Posted April 6, 2020 Author Share Posted April 6, 2020 is there a way to do this by php only? I read about JS but i didn't use it before. You can point to easy tutorial to do it with JS if not possible in php. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 6, 2020 Share Posted April 6, 2020 On your submit button do this <input type='submit' name='btn' value='Submit' onclick='return confirmDelete()'> Then in your script section create this function: function confirmDelete() { if (!confirm('Are you sure you want to do this delete?')) return false; else return confirm('Are you REALLY sure?'); } Quote Link to comment Share on other sites More sharing options...
ramiwahdan Posted April 6, 2020 Author Share Posted April 6, 2020 thanks for the help. your function will return Boolean of true or false. After i get the answer how to delete the records? before i had: <td><a href="deletestaff.php?GetID=<?php echo $userid?>">Delete</a></td> with your code i have: <td><button class="btn btn-primary" name="delete" onclick="return confirmDelete()">Delete</button></td> so after the function confirms yes for both, how to pass the link href="deletestaff.php?GetID=<?php echo $userid?>" thanks Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 6, 2020 Share Posted April 6, 2020 Well - how ARE you doing the delete? A PHP script, no? So use a form with a submit button and not a <button> and call the script that does the delete. 1 Quote Link to comment Share on other sites More sharing options...
ramiwahdan Posted April 6, 2020 Author Share Posted April 6, 2020 after running the function how to redirect to my deletestaff.php?id=??? Quote Link to comment Share on other sites More sharing options...
ramiwahdan Posted April 6, 2020 Author Share Posted April 6, 2020 got it, <form action="deletestaff.php?GetID=<?php echo $userid?>" method="post"> Thanks for the big help Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 6, 2020 Share Posted April 6, 2020 Actually the way to write that is echo "<form action='deletestaff.php?GetId=$userid' method='POST'>"; Stop using the in and out of php mode style. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.