简单实现AJAX/JSON交互 带后台
前台:
<a title="删除" href="javascript:;" onclick="member_del(this,'1')" class="ml-5"><i class="Hui-iconfont">删除</i>
加载JS:
<script type="text/javascript">
/*用户-删除*/
function member_del(obj,id){
layer.confirm('确认要删除吗?',function(index){
$.post("index.php?m=cangku&c=ware&a=del",{id:id},
function(data){
if(data.status=='ok'){
$(obj).parents("tr").remove();
layer.msg("已删除",{icon:1,time:1000});
}else{
layer.msg("删除失败,请重试!",{icon:2,time:1000});
}
}, "json");
});
}
</script>
后台PHP:
public function del() {
$id = $_POST['id'];
$this->db->table_name=get_tablepre().'cangku_ware';//读取信息表
$this->db->delete(array('wareid'=>$id));
//验证是否删除成功
if(count($this->db->select(array('wareid'=>$id)))>='1'){
echo json_encode(array("status"=>"no"));
}else{
echo json_encode(array("status"=>"ok"));
}
}