如何在同时删除多个记录时创建具有多个ID号的警报?

时间:2013-12-03 19:56:27

标签: javascript php mysql sql

我有一个记录表,其中每一行都有一个删除链接。单击删除链接时,将显示带有消息和记录ID的弹出确认。我在每一行都有一个复选框,可以检查多个复选框,并且可以单击一个按钮同时删除这些多个记录。

我想要做的是制作另一个弹出确认消息(对于删除多个作业按钮),但是要记录所有即将被删除的记录ID。这可能吗?

每一行的删除链接如下所示:

<a href="delete-job.php?id=' . $row['id'] . '" onclick="javascript: return confirm(\'Are you SURE you want to delete job ' . $row['id'] . '?\');">Delete</a>

包装表的表单如下所示:

<form name="viewjobsanddelete" method="post" action="delete-jobs.php"></form>

每行的复选框如下所示:

<input type="checkbox" name="checkbox[]" id="checkbox[]" value="' . $row['id'] . '" />

删除按钮如下所示:

<input name="delete" type="submit" id="delete" class="btn" value="Delete Multiple Jobs">

delete-job.php(用于删除单个记录/行)具有:

 // check if the 'id' variable is set in URL, and check that it is valid
 if (isset($_GET['id']) && is_numeric($_GET['id'])) {
 // get id value
 $id = $_GET['id'];

 // delete the entry
 $result = mysql_query("DELETE FROM jobs WHERE id=$id") or die(mysql_error()); 

 // redirect back to the view page
 header("Location: view-jobs.php");
 }
 else
 // if id isn't set, or isn't valid, redirect back to view page
 {
 header("Location: view-jobs.php");
 }

delete-jobs.php(用于删除多个记录/行)具有:

if(isset($_POST['delete'])) {
$checkbox = $_POST['checkbox'];
for($i=0;$i<count($_POST['checkbox']);$i++) {
    $del_id = $checkbox[$i];
    $sql = "DELETE FROM jobs WHERE id='$del_id'";
    //print $sql;
    $result = mysql_query($sql);
}
}
echo "<meta http-equiv=\"refresh\" content=\"0;URL=view-jobs.php\">";

那么我在哪里修改或添加什么来创建具有多个要删除的记录/行的id号的确认对话框窗口?

1 个答案:

答案 0 :(得分:0)

这是我如何处理它。

我在每个复选框中添加了class =“chk”,然后我使用了以下jquery ...

<script>
$(document).ready(function () { //if the page has been fully loaded
/* Get the checkbox values based on the class attached to each check box */

$("#viewjobsanddelete").submit(function(e){ //on submission of form

/* declare a checkbox array */
var chkArray = [];

/* look for all checkboxes that have a class 'chk' attached to it and check if it was checked */
$(".chk:checked").each(function() {
    chkArray.push($(this).val());
});

/* we join the array separated by a comma */
var selected;
selected = chkArray.join(',') + ",";

/* check if there are selected checkboxes, by default the length is 1 as it contains one single comma */
if(selected.length > 1){

    if (!confirm("You are about to delete these jobs: " + selected)) { //if click cancel instead of ok
        e.preventDefault(); //stop form submission
        return;
    }

}else{
    alert("Check one or more checkboxes first.");
    e.preventDefault(); //stop form submission
    return;
}

});

});
</script>