ServerSide $_POST is empty

ServerSide $_POST is empty

mgbtttmgbttt Posts: 4Questions: 2Answers: 0

Hello.
I have a DataTable with ServerSide configuration:

$("#tablaProductos").DataTable({
"Processing": true,
"serverSide": true,
"ajax": {
"url": "ajax/datatable-productos.ajax.php",
"type": "POST"
}
});

The network headers are:
General

Request URL: http://localhost/POS_TH/ajax/datatable-productos.ajax.php
Request Method: POST
Status Code: 200 OK
Remote Address: [::1]:80
Referrer Policy: no-referrer-when-downgrade

Response-Headers

Cache-Control: no-store, no-cache, must-revalidate
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 294
Content-Type: text/html; charset=UTF-8
Date: Fri, 22 Nov 2019 15:31:15 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive: timeout=5, max=83
Pragma: no-cache
Server: Apache/2.4.34 (Ubuntu)
Vary: Accept-Encoding

Request Headers

Accept: application/json, text/javascript, /; q=0.01
Accept-Encoding: gzip, deflate, br
Accept-Language: es-ES,es;q=0.9,en;q=0.8,ca;q=0.7
Connection: keep-alive
Content-Length: 1569
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Cookie: ...
Host: localhost
Origin: http://localhost
Referer: http://localhost/POS_TH/productos
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36
X-Requested-With: XMLHttpRequest

Form Data

draw: 1
columns[0][data]: 0
columns[0][name]:
columns[0][searchable]: true
columns[0][orderable]: true
columns[0][search][value]:
columns[0][search][regex]: false
columns[1][data]: 1
columns[1][name]:
columns[1][searchable]: true
columns[1][orderable]: true
columns[1][search][value]:
columns[1][search][regex]: false
columns[2][data]: 2
columns[2][name]:
columns[2][searchable]: true
columns[2][orderable]: true
columns[2][search][value]:
columns[2][search][regex]: false
columns[3][data]: 3
columns[3][name]:
columns[3][searchable]: true
columns[3][orderable]: true
columns[3][search][value]:
columns[3][search][regex]: false
columns[4][data]: 4
columns[4][name]:
columns[4][searchable]: true
columns[4][orderable]: true
columns[4][search][value]:
columns[4][search][regex]: false
columns[5][data]: 5
columns[5][name]:
columns[5][searchable]: true
columns[5][orderable]: true
columns[5][search][value]:
columns[5][search][regex]: false
columns[6][data]: 6
columns[6][name]:
columns[6][searchable]: true
columns[6][orderable]: true
columns[6][search][value]:
columns[6][search][regex]: false
order[0][column]: 0
order[0][dir]: asc
start: 0
length: 10
search[value]:
search[regex]: false

In datatable-productos.ajax.php the code is.
var_dump($_POST);

But the response is
array(0) { }

If I use type GET and var_dump($_GET); works fine and I have all the parameters sended by Datatable.
Why??
How can I use the type POST?

Thanks in advance.

Answers

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421

    To be honest. I don't really know why. But I took a look at my data tables definition. And I only use "POST" when I have something to post to the server ... Maybe that answers your question?!

    Example for "POST":

    var table = $('#tblInfomaContract').DataTable( {
        ajax: {
            url: 'actions.php?action=tblTable',
            type: 'POST',
            data: function ( d ) {
                var selected = otherTable.row( {selected: true} ); 
                if (selected.any()) {
                    d.contract_id = selected.data().contract.id;
                }
            }
        },        
        columns: [           
            { data: "table.external_id" }
        ]
    } );
    

    In this example the name of the $_POST variable in PHP is "contract_id" and it contains a value. So "d.contract_id" is the posted variable.

    In the next example I don't use "POST". (I think "GET" is the default anyway.) So I specified nothing. And yes: I don't need "POST" because I am not posting anything either!

    var secondTable = $('#tblContractFiltr').DataTable({
        dom: "Bti",
        fixedHeader: false,
        paging: false,
        retrieve: true,
        ajax: {
            url: 'actions.php?action=tblSecondTable'
        },
        columns: [
            {  data: "filtr_name" }, ....
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    This thread has been duplicated here - please post only once to avoid this confusion.

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421

    @colin, that is really annoying that people post their stuff twice. I thought there was really someone not getting help for a long time. Looks like I only wasted my time. If the forum rules say that duplication isn't allowed, I suggest you block users duplicating their questions. I mean if you don't get help you can still ask again in the same post. That should be good enough, shouldn't it?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    edited December 2019

    @rf1234 Yep, agreed, it does waste people's time and is frustrating for everyone involved, but if it's a one-off, or an accident, I'm happy to treat it as such. But yep, if it happens frequently we warn the users and then block them should it continue. That rarely happens fortunately.

This discussion has been closed.