【发布时间】:2018-02-07 12:46:27
【问题描述】:
我在使用 ajax 删除数据时遇到问题我首先尝试测试是否单击按钮进行警报操作,因此它不起作用。 这是我的控制器代码
public function indexajax()
{
$this->load->model("usersmodel");
$data["allu"]=$this->usersmodel->ShowAllUsers("users");
$data['pagetitle']=" -->All Users Using Ajax<--";
$this->load->view("template/admin/header",$data);
$this->load->view("users/allusersusingajax");
$this->load->view("template/admin/footer");
}
public function get_all_users()
{
if($this->input->post("action")=='FetchAllUserUingAjax1'){
$this->load->model("usersmodel");
$x= $data["allu"]=$this->usersmodel->ShowAllUsers("users");
foreach ($x as $a):
echo'<tr>
<td>'.$a->id.'</td>
<td>'.$a->username.'</td>
<td><button class="deletenew" id="'.$a->id.'">delete</button></td>
</tr>';
endforeach;
}
public function deleteusers()
{
$id=$this->input->post("id");
$this->load->model("usersmodel");
$this->usersmodel->deleteusers($id);
}
这是我的删除模型代码
public function deleteusers($id)
{
$this->db->where('id',$id);
if($this->db->delete("users")){
return true;
}else{
return false;
}
}
/**************这是查看页面*****/
<div class="userdataajax table-responsive">
<table class=" table table-responsive table-bordered">
<tr>
<th>#</th>
<th>name</th>
<th>image</th>
<th> full name</th>
<th>email</th>
<th>usertype</th>
<th>status</th>
<th>reg date</th>
<th>reg time</th>
<th>delete</th>
<th>edit</th>
<th>Activate</th>
<th>disactivate</th>
</tr>
</table>
</div>
<script>
$(document).ready(function () {
FetchAllUserUingAjax();
function FetchAllUserUingAjax() {
$.ajax({
url:'<?php echo base_url()?>Users/get_all_users',
method:"post",
success:function (data) {
$(".userdataajax table").append(data);
}
})
var action="FetchAllUserUingAjax1";
$.ajax({
url:"<?php echo base_url()?>Users/get_all_users",
method:"post",
data:{action:action},
success:function (data) {
$(".userdataajax table tr").not("table
tr:first").remove();
$(".userdataajax table").append(data);
Table();
}
})
};
$(".deletenew").on("click",function () {
alert("engex");//this alert not working
var id=$(this).attr('id');
$.ajax({
url:"<?php echo base_url()?>Users/deleteusers",
method:"post",
data:{id:id},
success:function () {
alert("deleted");
FetchAllUserUingAjax();
}
})
})
})
</script>
// 但如果我从控制器中删除 foreach (foreach) 并将其放入视图页面,则删除工作正常。我想知道我的问题是什么。
【问题讨论】:
-
您还没有真正说出您面临的问题。出错了?
-
没有删除发生
-
帮助我们帮助您。我们不应该提取信息。你检查过 MySQL 错误吗?您是否检查过尝试删除的已编译 SQL 查询?如果您在数据库中手动运行它会发生什么?做一些调试。
-
我的查询没有错误@Utkanos 如果您删除在 functon 的 get_all_users 控制器中找到的 foreach 并将其放在视图中,您会发现此代码正常工作
-
这是一个关于为什么它不起作用的合理解释。除了您尝试调用的方式外,代码一切都很好。您会看到,当您将其包含在视图(foreach 视图)中时,按钮会沿视图呈现,但如果您从控制器中使用它,它实际上是在加载视图后完成的。因此,在这种情况下,您将需要在函数上使用 jquery。 api.jquery.com/on
标签: php jquery ajax codeigniter