How do I pass a parameter to a server script?

How do I pass a parameter to a server script?

ostmalostmal Posts: 102Questions: 33Answers: 0

Pass the parameter from the JS script according to the documentation like this (https://editor.datatables.net/reference/option/ajax.data):

ajax:  {
        url: 'php/staff.php',
        data: {
            "user_id": 451
        }
    },

Sorry, but I didn't find how to accept it on the server. I do this:
$user_id = $_POST['user_id'];

DataTables swears, how is it correct?

This question has accepted answers - jump to:

Answers

  • ostmalostmal Posts: 102Questions: 33Answers: 0

    I do this:

    JavaScript:

    var table = $('#dt_08').DataTable({
                dom: 'Bfrtip',
                ajax: {
                    url: '/abc/w_lesson_datatable/dt-08/php/table.dt_08.php',
                    data: {
                        user_id : 11
                    }
                },
    …
    

    PHP:

    …
    ->where( 'dt_08.id_region', $_POST['user_id'] )
    …
    

    The script does not output the table and writes: «DataTables warning: table id=dt_08 - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1»

    Strange, like I'm doing everything right, like here:
    https://datatables.net/blog/2016-03-25#Server-side-(PHP)

    But, If I set a value:

    …
    ->where( 'dt_08.id_region', 11 )
    …
    

    The script is running.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    DataTables sends a GET request by default so either:

    $_GET['user_id']
    

    or if you are mixing with Editor, which does a POST by default, use:

    type: 'POST'
    

    in the ajax option for the DataTable.

    Allan

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736
    edited February 2020 Answer ✓

    I don't use PHP but I think $_POST is to retrieve the parameters from a POST request. You have set the Ajax request type to POST so it is sending the request as a GET. In this case I think you will need to use $_GET.

    Kevin

  • ostmalostmal Posts: 102Questions: 33Answers: 0

    Thank you very much! Really appreciate it!

This discussion has been closed.