Below is my delete code using php,I want to get confirmation after delete link was clicked from the user using php.
<?php
include('conn.php');
$query=mysql_query("DELETE FROM mark WHERE student_id='$_GET[st_id]'");
if($query)
{
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Succesfully deleted')
window.location.href='mark-details1.php';
</SCRIPT>");
}
else
{
echo "Check your Server";
}
?>
Please anyone can tell me how to do this? Thanks in advance!
you can use this javascript event on your html tag.
onclick="return confirm('you sure?');"
you can also use this :
if your link will send a get like "?delete=(id)"
<?php
include('conn.php');
if(isset($_GET['delete']) && is_numeric($_GET['delete'])==1){
echo (a page with a form with confirmation question content that will sent a get for example (?checked_delete=(id)));
}elseif(isset($_GET["checked_delete"]) && is_numeric($_GET["checked_delete"])==1){
// TODO : deleting record.
$query=mysql_query("DELETE FROM mark WHERE student_id='$_GET[checked_delete]'");
header("location:mark-details1.php")
}else{
echo (normal page);
}
?>
I suggest using Ajax. Here I will use jQuery to do it
<a class="wh" data-id="<?=$rows['student_id']?>" href="edit-mark.php?st_id=<?=$rows['student_id']?>" title="Edit">Edit Marks</a><span class="confirmation"></span>
using
$(function() {
$(".wh").on("click",function(e) {
e.preventDefault(); // cancel the click
$.get(this.href,function(data) { // does the student still exist?
if (confirm("delete" +data+"?")) {
$.get("otherphp.php?st_id="+$(this).data("id"),function(data) {
$(this).next().html(data); // show response
});
}
});
});
});
Or to hide the href from spiders
<a class="wh" href="onlyworkswithjavascript.html"
data-id="<?=$rows['student_id']?>" data-href="edit-mark.php?st_id=<?=$rows['student_id']?>"
title="Delete">Delete Marks</a><span class="confirmation"></span>
$(function() {
$(".wh").on("click",function(e) {
e.preventDefault(); // cancel the click
if (confirm("delete" +data+"?")) {
$.get($(this).data("href"),function(data) {
$(this).next().html(data); // show response
});
}
});
});