2011-08-03 68 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