POST variables not being passed to server side

POST variables not being passed to server side

ApolymoxicApolymoxic Posts: 15Questions: 4Answers: 0

I am having a hard time getting the serverside functionality to work correctly. I have been searching and trying to get this to work for about 2 days now... I have tried EVERYTHING I have found.

When I set the type to "POST", I don't get any variables passed over, and therefore, I cannot query SQL correct because I don't know start and length. When I remove POST or set it to GET, I get a funny object that I can parse and get the correct values, but I don't think it's supposed to be that way.

var contactTable = $('#contactList').DataTable({
                processing: true,
                serverSide: true,
                ordering: false,
                searching: {"regex": true},
                lengthMenu: [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
                pageLength: 50,
                ajax: {
                    "type": "POST",
                    "url": "api/get-contacts.php",
                    // "dataSrc":"",
                    "dataType": "json",
                    "contentType": 'application/json; charset=utf-8'
                },
                stateSave: true,
                columns: [
                    { "data": "check"},
                    { "data": "id" },
                    { "data": "first_name" },
                    { "data": "last_name" }
                ],
                columnDefs: [
                    {
                        "className": 'select-checkbox',
                        "targets": 0
                    },
                    {
                        "visible": false, "targets": [1, 5, 9, 10, 11, 13]
                    }
                ],
                select: {
                    style: 'os',
                    selector: 'td:first-child'
                },
                order: [[1, 'asc']]
            });

I have tried to place this in data, but it doesn't seem to help...

"data": function (data) {
                        // Grab form values containing user options
                        var form = {};
                        $.each($("form").serializeArray(), function (i, field) {
                            form[field.name] = field.value || "";
                        });

                        // Add options used by Datatables
                        var info = (contactTable == null) ? { "start": 0, "length": 50 } : contactTable.page.info();
                        $.extend(form, info);
                        return JSON.stringify(form);
                    },

All I want is to be able to access the variables on the serverside so I can make the correct SQL queries.

Can someone please help?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,354Questions: 26Answers: 4,776
    edited October 2018 Answer ✓

    Not sure what you are using for your server script. Whatever it is PHP, Python, etc you will need to setup the script to accept POST requests then parse the parameters.

    Here is a PHP server side processing example with POST. Take a look at the Server-side script tab.
    https://datatables.net/examples/server_side/post.html

    Kevin

  • ApolymoxicApolymoxic Posts: 15Questions: 4Answers: 0

    I apologize... I knew I was forgetting something.

    This is the top of my PHP page for serverside

    require __DIR__ . '/../appCode/functions.php';
    
    
    var_dump($_REQUEST);
    
    $startIndex = $_POST['start']; //Start Index
    $length = $_POST['length']; //Limit of Records to fetch
    $searchString = $_POST['search']['value']; // String value to search in database
    $orderByColumn = $_POST['order'][0]['column']; //Order of the Column
    $orderByDirection = $_POST['order'][0]['dir']; //Direction of the Column Asc or Desc
    
    

    I get errors on this page saying that 'start', 'length', etc... are all undefined. When I dump $_REQUEST and $_POST, there is nothing.

    That example doesn't help me much because it doesn't show how to access the variables in the PHP page. I feel like this is something simple that I am just over looking...

  • ApolymoxicApolymoxic Posts: 15Questions: 4Answers: 0
    edited October 2018

    Maybe it's the ssp.class.php? I don't have that... Will that help access the variables?

    I found it here https://github.com/DataTables/DataTables/blob/master/examples/server_side/scripts/ssp.class.php but I fail to see how that will help me. I just need access to the variables, not my DB. I have my queries already created.

  • ApolymoxicApolymoxic Posts: 15Questions: 4Answers: 0

    So, I ended up stripping the whole thing down and now it works. I am not sure what I was doing.

    datatables.php

    ajax: {
                        type: "POST",
                        url: "api/get-contacts.php"
                    },
    

    serverside.php

    require __DIR__ . '/../appCode/functions.php';
    
    $startIndex = $_REQUEST['start']; //Start Index
    $length = $_REQUEST['length']; //Limit of Records to fetch
    $searchString = $_REQUEST['search']['value']; // String value to search in database
    $orderByColumn = $_REQUEST['order'][0]['column']; //Order of the Column
    $orderByDirection = $_REQUEST['order'][0]['dir']; //Direction of the Column Asc or Desc
    

    Thank you for your help.

This discussion has been closed.