2014-10-11 72 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