我的代码对每个表都重复。ajax函数和文件delete.php每次我想删除时都会重复。例如,我在mysql中有100个表,我必须制作100个delete.php
我是否可以对所有用户使用1 delete.php,也可以使用ajax函数?
PHP代码:
echo"<td><a href='#' onclick='doConfirm(".$id.")'>Delete</a></td>";
Ajax代码:
function doConfirm(id)
{
var xhttp;
var ok = confirm("Are you sure to Delete?")
if (ok)
{
xhttp= new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById('show1').innerHTML=xhttp.responseText;
}
}
xhttp.open("GET", "delete.php?id=" + id,true);
xhttp.send();
}
}
delete.php
$f0=$_GET['id'];
delete($f0);
发布于 2015-10-09 01:31:34
将另一个param作为tablename传递给onclick='doConfirm(".$id.")'
,然后在GET方法上传递表名作为查询字符串,然后在单个delete.php文件中从传递的文件中删除。
行政长官:echo"<td><a href='#' onclick='doConfirm(".$id.", "table1")'>Delete</a></td>";
Ajax
function doConfirm(id, tableName)
{
var xhttp;
var ok = confirm("Are you sure to Delete?")
if (ok)
{
xhttp= new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById('show1').innerHTML=xhttp.responseText;
}
}
xhttp.open("GET", "delete.php?id=" + id + "table=" + tableName,true);
xhttp.send();
}
}
希望这能帮上忙。
发布于 2015-10-09 01:29:49
创建一个主文件,该文件将根据请求执行。例如,要在tbl_book中删除,我们的url将是
xhttp.open("GET", "delete.php?target=book&id=" + id,true);
现在在delete.php中,在交换机或如果其他阻塞
$target=$_REQUEST['target'];
switch(target)
{
case 'book': //your delete query for tbl_book
break;
case 'emp': //your delete query for tbl_emp
break;
}
上面的例子只是给出了一个按要求执行..Change的想法。
https://stackoverflow.com/questions/33033735
复制