简体   繁体   中英

Post Data in php using datatables javascript serverside

i am trying to create a filter function from my datatables, how can i post the value that i want coming from the javascript to php using the datatables function?

here is my sample code from my datatables

    var oTable = $('#datatables').DataTable({
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": base_url + "link",
        "fnServerParams": function (data) {
            data.filter_start_date = $('#filter_start_date').val();
            console.log(data);
        },
        dom: 'lBfrtip',
        buttons: [
            'excel'
        ],
        "lengthMenu": [[10, 20, 50, 100, 300, -1], [10, 20, 50, 100, 300, "All"]],
        "pagingType": "full_numbers",
        "language": {
            "paginate": {
                "previous": 'Prev',
                "next": 'Next',
            }
        },
        "bAutoWidth": false,
        "aaSorting": [[ 0, "desc" ]],
    });

and the data.filter_start_date value is i want it to be post from my php. thank you in advance, i am just really stuck with this problem or am i just doing it wrong.

On php you will get a param filter_start_date which will be sent on every datatables ajax request . Just use it to filter your results.

And in javascript you can do something like this to reload the table

$('#filter_start_date').change(function() {
    oTable.ajax.reload();
});

EDIT:

Here's exactly how I use it:

$('#table').DataTable({
   //configs
   ajax: {
      url: baseUrl('/ajax/load-docs'),
      type: 'post',
      data: function(d) {
          d.initial_date = $('#initial_date').val();
      }
   }
});

and in php

$date = $this->getParam('initial_date'); //getParam from Zend_Framework Controller

in your case you can use $_POST['filter_start_date']

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