我試圖用AJAX和Code Igniter從數據庫中刪除用戶。當我點擊刪除鏈接時,用戶被刪除,但頁面被重定向並顯示成功消息。 AJAX似乎不能在我的代碼中工作。 這裏的HTML:使用AJAX和Codeigniter從數據庫中刪除用戶
<table class="table table-hover">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Username</th>
<th>Password</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach($users as $u){?>
<tr>
<td><?php echo $u['id']; ?></td>
<td><?php echo $u['firstname']; ?></td>
<td><?php echo $u['lastname']; ?></td>
<td><?php echo $u['email']; ?></td>
<td><?php echo $u['username']; ?></td>
<td><?php echo $u['password']; ?></td>
<td>
<a href="#" >Edit</a> |
<?php $id=$u['id'];?>
<a href="<?php echo site_url("users/delete/$id")?>" class="delete">Delete</a>
</td>
<?php }?>
</tr>
</tbody>
</table>
和這裏的AJAX:
$(document).ready(function(){
$(".delete").click(function(){
alert("Delete?");
var href = $(this).attr("href");
var btn = this;
$.ajax({
type: "GET",
url: href,
success: function(response) {
if (response == "Success")
{
$(btn).closest('tr').fadeOut("slow");
}
else
{
alert("Error");
}
}
});
})
});
,最後控制器功能刪除用戶笨
public function delete($id)//for deleting the user
{
$this->load->model('Users_m');
$delete=$this->Users_m->delete_user($id);
if($delete)
{
echo "Success";
}
else
{
echo "Error";
}
}
我在哪裏做的錯誤?
你是正確的event.preventDefault()幫助。我試圖添加alert(),說用戶已被刪除,但fadeOut()不起作用。這是爲什麼? – sabin 2013-04-21 09:58:28
嗯,fadeOut的代碼看起來對我來說是正確的。我不確定表格行是否可以淡化。也許你可以用'.hide()'來代替它。 – Aeolun 2013-04-22 12:32:41