我正在尝试使用 AJAX 和 Code Igniter 从数据库中删除用户。当我点击删除链接时,用户会被删除,但页面会重定向并显示成功消息。我的代码似乎无法使用 AJAX。
以下是 HTML 代码:
<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");
}
}
});
})
});
最后,在Codeigniter中删除用户的控制器函数。
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";
}
}
我在哪里犯了错误?
.hide()
代替。 - Aeolun