使用ajax和codegniter删除数据

时间:2018-02-07 12:46:27

标签: php jquery ajax codeigniter

我在使用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)并将其放入视图页面,则删除正在运行我想知道我的问题是什么

2 个答案:

答案 0 :(得分:1)

当您为删除按钮设置单击处理程序时,这些按钮在页面中不存在(但是......),因为它们正在加载另一个ajax请求。

要绑定稍后添加到页面的按钮,您可以使用事件委派。为此,您需要更改单击处理程序:

$(".deletenew").on("click",function () {

为:

$('body').on('click', '.deletenew', function () {

我已将其委托给body元素,但您可以使用任何包含按钮的元素,并且已在页面加载/ DOM就绪时可用。

答案 1 :(得分:0)

       //change Controller function 

     public function get_all_users()

     {

        if($this->input->post("action")=='FetchAllUserUingAjax1')
    {

         $this->load->model("usersmodel");
        // print_r($this->input->post("action"));
         echo ($this->input->post("action"));
        $data["allu"]=$this->usersmodel->ShowAllUsers("users");
        $this->load->view("template/admin/header");
       //make another view show_users,make a button in page
       //alluersusing ajax give that button id 'but'
        $this->load->view("users/show_users,$data");

        $this->load->view("template/admin/footer");

     }


 //copy paste the code below in  show_users view


        foreach ($x as $a):

                         echo'<tr>

                    <td>'.$a->id.'</td>

                    <td>'.$a->username.'</td>

        <td><button class="btn btn primary" onclick=del(<?php echo '.$a->id.' ?>)>delete</button></td>

                    </tr>';
                     endforeach;


//Ajax Delete function

<script>

 function del(id) {


        $.ajax({
            type: "POST",
            url: '<?php echo base_url('Users/deleteusers')?>' +"/"+ id,
            success: function (data) {
                $("#get").html(data);


            }
        });

    }
</script>