6.10ajax举例

$('.del').click(function(){
        var id = $(this).data('id');
        var that = $(this);
        layer.confirm('确定删除'+id+'吗?',{icon: 3,title: '提示'},
        function(index){
          $.ajax({
            //ajax用法
            url:'/ajax.php',
            //与哪个文件进行传递
            type:'post',
            //传递方式
            data:{id:id},
            dataType:'json',
            //返回类型
            success:function(res){
              //执行函数
              console.log(res);
              layer.close(index);
              if (res.code==1){
                location.reload();
              }else{
                layer.msg('删除失败!');
              }
            }

          });
        });
在HTML文件适当位置用class为del的标签即可应用
ajax.php
<?php
if ($_POST){
    $ids = $_POST['id']; 
    $conn = new mysqli('localhost','root','root','ceshi');
    $sql = "select * from product where id = $ids";
    $res = $conn->query($sql);
    //连接数据库老一套
    $arr = [];
    if ($res){
        $arr['code'] = 1;
        //删除成功输出1
    }else{
        $arr['code'] = 2;
        //删除失败输出2
    }
    echo json_encode($arr);
    $conn->close();
}



?>
posted @ 2020-06-10 19:47  HighKK  阅读(158)  评论(0编辑  收藏  举报