2014-10-11 37 views
2

我想做一個警報系統,在用戶屏幕上彈出一個警報,但我有一個主要問題,我不知道如何刪除該div完全,我知道我需要從數據庫中刪除它,但這就是我遇到麻煩,我似乎無法找到一種方法來從數據庫中抓取ID的ID用於從數據庫。 這是我的嘗試。點擊div後jquery從數據庫中刪除數據

<script src="//code.jquery.com/jquery-1.10.2.js"></script> 
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script> 
<script>  
$(document).ready(function() 
{ 

    $(".alert").draggable();  
    $(".alert").click(function() 
    {   
     var test = $("#warning").val(); 
     alert(test); 
     $(this).fadeOut("fast"); 

    }); 
}); 
</script> 
<style> 
#warning 
{ 
    width:150px; 
    height:150px; 
    border:1px solid red; 
    background-color:rgb(230, 30, 30); 
    border-radius:5px; 
    padding:5px;  
} 
#notice 
{ 
    width:150px; 
    height:150px; 
    border:1px solid yellow; 
    background-color:rgb(230, 80, 30); 
    border-radius:5px; 
    padding:5px;  
} 
#generic 
{ 
    width:150px; 
    height:150px; 
    border:1px solid grey; 
    background-color:rgb(150, 150, 150); 
    border-radius:5px; 
    padding:5px;  
} 
.blue 
{ 
    font-weight:bold; 
} 
</style> 
<?php 
function display_alerts($user_id) 
{ 
    $connect = mysqli_connect("localhost", "root", "asdfghjkl", "database"); 
    $query = mysqli_query($connect, "SELECT * FROM `alerts` WHERE `user` = '$user_id'"); 
    while($row = mysqli_fetch_assoc($query)) 
    {  
    ?>   
     <div id="<?php echo $row["style"]; ?>" value = "<?php echo $row["id"]; ?>" class="alert"><?php echo $row["message"]; ?></div> 
     <?php  
    }  
} 
display_alerts("10"); 
?> 
+0

爲什麼不直接使用的形式在這種情況下,fyi'.val()'用於表單輸入。但如果你真的堅持通過這些div來完成這項工作,你可以使用ajax – Ghost 2014-10-11 07:07:05

+0

@Ghost 這就是我需要一些幫助,我對Ajax不太好,而且我對JQuery也不是太好。我加了'

',但是結果仍然沒有變化。 – user3867184 2014-10-11 07:11:36

+0

然後只是使用一個正常的形式。爲什麼複雜的東西?只是像你一樣循環條目,並將它們包裝在表單標籤中。當然這只是一個開始,你必須處理表單並使用刪除語句。只使用''單獨不會刪除這些記錄 – Ghost 2014-10-11 07:13:25

回答

1

如果你想$.ajax()路線,只是調用PHP將處理過程:

$(".alert").click(function(){   

    var id = $(this).attr('value'); // where the id resides on the markup 
    $.ajax({ 
     url: 'delete_alert.php', 
     type: 'POST', 
     dataType: 'JSON', 
     data: {id : id}, 
     success: function(response) { 
      alert(response.message); 

     } 
    }); 

    $(this).fadeOut("fast"); 
}); 
在你的PHP

然後(delete_alert.php):

if(isset($_POST['id'])) { 
    $message = ''; 
    $db = new mysqli("localhost", "root", "asdfghjkl", "database"); 
    $id = $db->real_escape_string($_POST['id']); 
    $query = $db->query("DELETE FROM alerts WHERE id = '$id' "); 
    if($query->affected_rows > 0) { 
     $message = 'delete successful'; 
    } else { 
     $message = 'delete failed'; 
    } 

    echo json_encode(array('message' => $message)); 
    exit; 
} 
+0

非常感謝,我以前從來沒有用過Ajax,所以我很高興能嘗試一些新的東西。 – user3867184 2014-10-11 07:29:59

+0

@ user3867184對此有所幫助 – Ghost 2014-10-11 07:31:06

1

你可以得到$(this).attr("id")

的ID,然後在你有這個ID傳遞給PHP頁面刪除。正如Ghost建議的那樣,您可以使用表單或撥打jQuery ajax

+0

謝謝,我想我可以通過PHP來處理它(這是我知道的唯一的Ajax,因爲我有)讓我只是測試一些更多的,我會接受你的答案c: – user3867184 2014-10-11 07:13:24

+0

快速的問題,這是我刪除數據的方式,是通過XMLHTTPRequest,這是我的代碼 'xhttp = new XMLHttpRequest(); \t \t xhttp.open(「http://localhost/LID/remove_moderator_alert.php?alert_id =」+ alert_id +「」,true); ('它在一個新的函數中)我得到這個錯誤:'未捕獲的SyntaxError:無法在'XMLHttpRequest'上執行'open':'http:// localhost/LID/remove_moderator_alert。 php?alert_id = 1'不是一個有效的HTTP方法。「任何想法如何解決它? – user3867184 2014-10-11 07:24:47

+0

檢查鬼魂的答案。這也是它使用它的方式。 – Barry 2014-10-11 07:29:09