Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post the parameter in ajax call of jquery datatable

Tags:

As of now I am passing parameter along with URL in ajax call of data table.

But I want to pass it as POST method, please anyone one help me regarding parameter passing in post method, here's my trial code:

// Sending through GET
var $table = $('#example').dataTable( 
    "processing": true,
    "serverSide": true,
    "bDestroy": true,
    "bJQueryUI": true,
    "ajax": 'getResult.php?formName=afscpMcn&action=search&mcn_no='+mcnNum+'&cust_nm='+cust_num+'&emp_id='+emp+''
});
like image 287
chaya Avatar asked Aug 26 '14 12:08

chaya


2 Answers

Just pass it like a normal jQuery ajax in POST fashion.

The structure should look like this:

ajax: { type: 'POST', url: <path>, data: { your desired data } }

Example:

var $table = $('#example').dataTable( 
    "processing": true,
    "serverSide": true,
    "bDestroy": true,
    "bJQueryUI": true,
    "ajax": {
        'type': 'POST',
        'url': 'getResult.php',
        'data': {
           formName: 'afscpMcn',
           action: 'search',
           // etc..
        },
    }
});

In PHP, just access the POST indices as usual (just the straightforward approach):

getResult.php

$form_name = $_POST['formName'];
// the rest of your values ...

DataTables manual entry

like image 144
Kevin Avatar answered Oct 29 '22 01:10

Kevin


You can try this way:

$('#example').dataTable( {
  "ajax": {
    "url": "data.json",
    "data": function ( d ) {
        d.extra_search = $('#extra').val();
    }
  }
});

https://datatables.net/reference/option/ajax.data

like image 20
bharat Avatar answered Oct 29 '22 01:10

bharat