0

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.

1
  • Do you want go get that value from javascript into php ? or you want to send it to javascript from php ? data.filter_start_date = $('#filter_start_date').val(); Commented Jan 8, 2020 at 13:05

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

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.