简体   繁体   中英

Unable to fetch data from the table using AJAX and codeigniter

I am Creating a HRM+CRM (Human Resource Management and Customer relation management). Now I stuck in a error. I want to generate the Invoice for Each customer but I am unable to fix this problem. please help me and thank you in advance.

View - controller (Admin/gstInvoice)

<div class="row">
                <form id="myform" method="POST" action="">
                    <div class="col-lg-6">
                        <select id="select" class="form-control">
                            <option value="No Data">-- Select Client --</option>
                            <?php foreach ($client as $clientx): ?>
                                <option id="<?= $clientx->cid; ?>" name="<?= $clientx->client_name; ?>"><?= $clientx->client_name; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-lg-6">
                        <input type="button" class="submit btn btn-success" id="submit" name="Submit" Value="Submit"/>
                    </div>
                </form>
            </div>
            <div id="result"></div>
            <script>
                $(document).ready(function(){
                $('#submit').click(function(event){
                    event.preventDefault();
                    $.ajax({
                        url : "Invoice/invoiceData",
                        method : 'POST',
                        datatype:'html',
                        success:function(Result){
                            $('#result').html(Result)
                        }
                    })
                })
            })
            </script>

Now I want when I select a Client and press Submit the it load It's all old Invoices and also show a button for create new Invoice.

I have used the php code for creating the Salary slip of each employee, And, Now I want to fetch data for every Invoice table without refreshing the page

What I want you can understand through below Images.

I want to run a Ajax script to fetch the data from database and display in same page without page reloading.

My Salary table for each employee

在此处输入图片说明

After clicking on any Employee - All salary Slip

在此处输入图片说明

please help me.

You are not sending form data with ajax request. Use $('#myform').serialize() to send data

Also you are not giving name attribute to <select> tag

Use <select id="select" class="form-control" name="client">

Do like this

$(document).ready(function(){
   $('#submit').click(function(event){
       event.preventDefault();
       $.ajax({
          url : "<?= base_url('Invoice/invoiceData') ?>",
          method : 'POST',
          data: $('#myform').serialize(),
          dataType:'html',
          success:function(Result){
              $('#result').html(Result)
           }
       });
     });
   });

Controller

Then in controller function get form data like this

$client = $this-input->post('client');
<div class="row">
    <form id="myform" method="POST" action="">
        <div class="col-lg-6">
            <select id="select" name="client" class="form-control">
                <option value="No Data">-- Select Client --</option>
                <?php foreach ($client as $clientx): ?>
                    <option value="<?= $clientx->cid; ?>" id="<?= $clientx->cid; ?>" name="<?= $clientx->client_name; ?>"><?= $clientx->client_name; ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-lg-6">
            <input type="button" class="submit btn btn-success" id="submit" name="Submit" Value="Submit"/>
        </div>
    </form>
</div>
<div id="result"></div>
<script>
    $(document).ready(function(){
    $('#submit').click(function(event){
        event.preventDefault();
        $.ajax({
            url : "<?= base_url('Invoice/invoiceData') ?>",
            data: $('#myform').serialize(),
            method : 'POST',
            dataType:'html',
            success:function(Result){
                $('#result').html(Result)
            }
        })
    })
})
</script>

<!-- Controller -->
<?php
public function invoiceData() { 
    $client = $this->input->post('client'); 
    $this->view->load('Admin/sample',$client, TRUE); 
}

Edited your input form and controller, please check.

Model Code

  public function get_cities(){

  $this->db->order_by('city_id','desc');
  $this->db->limit('1000');
  $this->db->select('*,cities.name as city_name');
  $this->db->from('cities');
  $this->db->join('states','states.id=cities.state_id','left');
  $this->db->join('countries','countries.id=states.country_id','left');
  $data=$this->db->get();
  return $data->result_array();
  }
   private function get_datatables_query_city(){
    $column_search = array('cities.name','states.name','countries.country_name');
    $order = array('city_id' => 'desc');
    $column_order = array(null, 'city_name', 'sname', 'country_name', null);
    $this->db->select('cities.city_id,cities.name as city_name,states.name as sname,states.id as state_id,countries.id as country_id,countries.country_name');
     $this->db->from('cities');
     $this->db->join('states','states.id=cities.state_id','left');
     $this->db->join('countries','countries.id=states.country_id','left');
     $i = 0;
     foreach ($column_search as $item)
     {
        if($_POST['search']['value']) // if datatable send POST for search
            {
                if($i===0)
                {
                    $this->db->group_start();
                    $this->db->like($item, $_POST['search']['value']);
                }
                else
                {
                    $this->db->or_like($item, $_POST['search']['value']);
                }
                if(count($column_search) - 1 == $i)
                $this->db->group_end();
        }
        $i++;
    }
    if(isset($_POST['order']))
    {
        $this->db->order_by($column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
    }
    else if(isset($order))
    {
        $order = $order;
        $this->db->order_by(key($order), $order[key($order)]);
    }
}
function get_datatables_city()
{
    $this->get_datatables_query_city();
    if($_POST['length'] != -1)
        $this->db->limit($_POST['length'], $_POST['start']);
    $query = $this->db->get();
    return $query->result();
}
function count_filtered_city()
{
    $this->get_datatables_query_city();
    $query = $this->db->get();
    return $query->num_rows();
}
public function count_all_city()
{
    $this->db->from('cities');
    return $this->db->count_all_results();
} 

Controller Code

 public function city_list(){
        $this->load->view('admin/header');
        $this->load->view('admin/city');
        $this->load->view('admin/footer');
    }
    public function city_ajax(){
        $list = $this->Admin_model->get_datatables_city();
        $data = array();
        $no = $_POST['start'];
        foreach ($list as $city){
            $no++;
            $row = array();
            $row[] = $no;
            $row[] = $city->city_name;
            $row[] = $city->sname;
            $row[] = $city->country_name;
            $row[] = '<div class="d-flex align-items-center card-action-wrap">
            <div class="inline-block dropdown">
            <a class="dropdown-toggle no-caret" data-toggle="dropdown" href="#" aria-expanded="false" role="button"><i class="ion ion-ios-more"></i></a>
            <div class="dropdown-menu dropdown-menu-right" x-placement="top-end" style="position: absolute; transform: translate3d(-214px, -161px, 0px); top: 0px; left: 0px; will-change: transform;">
            <a class="dropdown-item" href="'.base_url().'Adminhome/city_edit/'.$city->city_id.'" ><i class="fas fa-edit read-icon"></i> Edit</a>
            <a id="mybutton" href="javascript:void(0);" onclick="citydelete('.$city->city_id.')" class="dropdown-item text-danger remove"  data-toggle="modal" data-target="#delete" data-id="'.$city->city_id.'"><i class="fas fa-trash-alt read-icon text-danger"></i> Delete</a>
            </div>
            </div>
            </div>';
            $data[] = $row;
        }
        $output = array(
            "draw" => $_POST['draw'],
            "recordsTotal" => $this->Admin_model->count_all_city(),
            "recordsFiltered" => $this->Admin_model->count_filtered_city(),
            "data" => $data,
        );
        echo json_encode($output);
    }

view code

            <div class="section-body">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-striped" id="example">
                                        <thead>
                                            <tr>
                                                <th>Sr.Number</th>
                                                <th>City Name</th>
                                                <th>State Name</th>
                                                <th>Country</th>
                                                <th>Manage</th>
                                            </tr>
                                        </thead>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </div>
    <div class="modal fade" id="delete" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-body">
                    Are you sure want to delete.
                </div>
                <div class="modal-footer bg-whitesmoke br">
                    <a id="confirm-button"> <button type="button" class="btn btn-danger">Delete</button></a>
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                </div>
            </div>
        </div>
    </div>
    <div class="alert alert-primary show2" role="alert" id="snackbar2" style="visibility: hidden">
        Successfully Deleted
    </div>
    <script>
        function citydelete(id){
            $('#delete').modal('show');
            rowToDelete = $(this).closest('tr');
            $('#confirm-button').click(function(){
                 $.ajax({
                    url: '<?php echo base_url();?>/Adminhome/city_delete',
                    type: 'GET',
                    data: {id: id},
                    success: function (data){
                        $("#"+id).remove();
                        var table = $('#example').DataTable();
                        table.ajax.reload( null, false );
                        $("#delete").modal('hide');
                        document.getElementById("snackbar2").style.visibility = "visible";
                        setTimeout(function() {
                            document.getElementById('snackbar2').style.visibility = 'hidden';
                        }, 3000);
                    }
                });
                })
        }
    </script>
    <script type="text/javascript">
        var table;
        $(document).ready(function() {
            table = $('#example').DataTable({ 
                "processing": true,
                "serverSide": true,
                "stateSave": true,
                "order": [],
                "ajax": {
                    "url": "<?php echo site_url('Adminhome/city_ajax')?>",
                    "type": "POST"
                },
                "columnDefs": [
                { 
                    "targets": [ 0 ],
                    "orderable": false,
                },
                ],
            });
        });
    </script>   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM