2011-08-03 67 views
0

我有一段时间的脚本在我的页面上运行,我想为每个结果添加一个删除按钮,但在他们将能够删除之前,我想要一个JavaScript弹出窗口询问他们是否确定要删除它。只是问最好的办法是什么? (我将使用Ajax运行删除功能,因为我不想从删除后的页面导航)。PHP用JS确认框删除按钮?

这是我使用的代码,到目前为止一切正常 - 结果显示,删除按钮弹出确认框。现在我被卡住了。

弹出:

<script type="text/javascript"> 
window.show_confirm = function() { 
var r = confirm("Are you sure you want to delete?"); 
if (r == true) { 
    alert(" I NEED TO DO SOMETHING HERE? "); 
} else { 
    alert("The item won't be deleted."); 
} 
} 
</script> 

PHP虽然:

<?php 
while ($row_apps = mysql_fetch_array($result_apps)) 
{ 
    if (($row_apps['type'] == 'ar') && ($row_apps['client'] == NULL)) { 
echo '<center> 
<div id="tab_listing"> 
<table width="248" border="0" cellspacing="10px" cellpadding="0px"> 
    <tr> 
    <td width="100%" colspan="3"><div class="tab_listing_header">'.$row_apps['app_name'].'</div></td> 
    </tr> 
    <tr> 
    <td class="tab_listing_values"><b>App ID: </b>'.$row_apps['app_id'].'</td> 
    <td class="tab_listing_values"><b>Users: </b>'.$row_apps['users'].'</td> 
    </tr> 
</table> 
<div id="edit">Edit</div> 
<a href="#" onclick="return show_confirm()"><div id="delete"></div></a> 
</div> 
</center><br>'; 
} 
} 
?> 

如果有人能提出一个办法做到这一点生病感激:)

+0

呼叫一个XMLHttpRequest到删除文件的PHP文件,你可能希望将参数添加到该函数来获取APP_ID删除。 – Wrikken

回答

1

起初,你不应该在循环中使用相同的ID!如果你有多个元素,请使用class!但它可能是有用的也有一个ID,所以它绑定到你的输出中的独特元素:

<div id="_<?php echo $row_apps['app_id']; ?>" class="tab_listing"> 

为了引用您要删除的条目,你必须通过类似的ID给删除功能,例如

<a href="#" onclick="return show_confirm(<?php echo $row_apps['app_id']; ?>)"><div id="delete"></div></a> 

所以脚本必须是这样的:

<script> 
window.show_confirm = function(id) { //notice the passed in parameter 
var r = confirm("Are you sure you want to delete?"); 
if (r == true) { 
    alert(" I NEED TO DO SOMETHING HERE? "); 
    //now do an ajax-request with the id you want to delete 
    //after the request, do something with the div ('_'+id), 
    //e.g. hide or add a text. 
    //with jQuery it's 1 line of code: 
    // $.post('delete.php', {'id':id}, function(data){$('#_'+id).html('Deleted!')}); 
} else { 
    alert("The item won't be deleted."); 
} 
} 
</script> 

对于AJAX的东西,我建议使用jQuery - 它会为你节省大量的工作!

而且delete.php可能是这个样子:

<?php 

if(isset($_POST['id'])) { 
    $id = $_POST['id']; 
    //do something with the id... 
} 

?> 
+0

编辑:我明白了!非常感谢你,我的朋友。 – Ricardo

+0

@Rico Steel不客气! – Quasdunk