1

I am new in codeigniter I want to retrieve data into table using ajax but no result found please help me to solve my problem

this is controller code

public function indexajax()
      {


          $this->load->model("usersmodel");
         $data['pagetitle']=" -->All Users Using Ajax<--";



         $this->load->view("template/admin/header");
          $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");
             // print_r($this->input->post("action"));
              echo ($this->input->post("action"));
              $data["allu"]=$this->usersmodel->ShowAllUsers("users");
 foreach ($allu as $a):
        echo'<tr>
            <td>'.$a->id.'</td>
            <td>'.$a->username.'</td>
           <td><button class="deletenew" id="'.$a->id.'">deletenew</button></td>

        </tr>';
        endforeach;



          }




      }


}

this is my model code

public function ShowAllUsers()
        {
            //$this->db->order_by("id", "desc");
        //  $this->db->limit(20);
            $sql=$this->db->get("users");
            return $sql->result();

        }

This is my view code

this is my HTML

<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>

this is my ajax calling trying to retrieve and delete data

<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 () {
            var id=$(this).attr('id');
            $.ajax({
                url:'<?php echo base_url()?>Users/deleteusers',
                method:"post",
                data:{id:id},
                success:function () {
                    FetchAllUserUingAjax();

                }

            })
        })

    })

</script>

1 Answer 1

1
                 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");
                }
            <script>
             $("#but").click(function() {
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url()?>Users/get_all_users",
                        success: function(data) {
                            $("#get").html(data);
                            //console.log(data);
                        }
                    });
                });

            </script>

        //add below code to 'show_users' page or view
       <?php foreach ($allu as $a):
                echo'<tr>
                    <td>'.$a->id.'</td>
                    <td>'.$a->username.'</td>
                   <td><button class="deletenew" id="'.$a->id.'">deletenew</button></td>

                </tr>';
                endforeach;

//hope this will help
Sign up to request clarification or add additional context in comments.

1 Comment

You Welcome :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.