我正在构建我之前提出并解决的一个问题:前端 mysql,删除一行
基本上,我有一个用户可以查看数据库的前端。 我想要一个可以为多行选择的复选框,而不是在每行旁边都有一个删除按钮。 然后,用户只需单击一个删除按钮,所有选定的行都将被删除。 我根本不了解 php 和 mysql,所以我不确定如何处理我已经拥有的代码。
目前,onclick 调用删除函数。 谁能帮忙?
我有一个 php 文件,它将 mysql 数据的 html 输出到一个长强的,我需要更改的部分是:
$display_string .= "<td class='blank'><input type='"button'" VALUE='"Delete'" onclick='delFunction(" . $row['ID'] . ")' ></td>";
接下来我的删除功能:
function delFunction(ID){
// confirm delete
if (!confirm('"Are you sure you want to delete?'")) return false;
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject('"Msxml2.XMLHTTP'");
} catch (e) {
try{
ajaxRequest = new ActiveXObject('"Microsoft.XMLHTTP'");
} catch (e){
// Something went wrong
alert('"Your browser broke!'");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var queryString = '"?ID='" + ID
ajaxRequest.open('"GET'", '"delete_row.php'" + queryString, true);
ajaxRequest.send(null);
}
为了理解您的问题,我正在为前端和后端发布一些代码
前端示例代码
<body>
<form action="delete.php" method="post">
<input type="checkbox" name="del_chk[]">Item1
<input type="checkbox" name="del_chk[]">Item2
<input type="checkbox" name="del_chk[]">Item3
<input type="checkbox" name="del_chk[]">Item4
.
.
<input type="submit">
</form>
................
您的后端代码现在将是...
<?php
if(isset($_POST['del_chk'])){
$chkbox=$_POST['del_chk'];
foreach($chkbox as $key=>$value) {
//Now you can get your value and use it in mysql delete statements
//for example
$del_query="DELETE FROM `yourtable` WHERE `pri_key`=$value;";
if(mysql_query($del_query)) {
echo "Successful Deletion of: ".$value;
}
else
{
echo "Unsuccessful Deletion of: ".$value;
}
} //end foreach
}
?>
我对ajax了解不多,但是你可以使用ajax来调用这个页面。