Retrieve Session from php and use in DataTable as query variable

Retrieve Session from php and use in DataTable as query variable

rainolfrainolf Posts: 56Questions: 6Answers: 0

Hello,
i'm working on this:

$.ajax({
url: "http://localhost/Web%20Projects/Conference%20Platform/include/pages/sessions.php",
dataType: 'json',
cache: false,
success: function(data) {
var owner = data.owner;
session = data.session;
//console.log(session.user_id); // deal with session
//$("#soandso").html(owner); //Insert who's typing into the #soandso
var username = session.toLowerCase();
$("#soandso").html(username); //Insert who's typing into the #soandso
}
});

This works as generic jquery ajax get to retrieve a session variable from php.

Now i need to do the same in DataTables. This will makes me retrieve an important information that i will use in where clause on my database query.

Starting from the example:

$('#example').dataTable( {
"ajax": {
"url": "data.json",
"type": "POST"
}
} );

which is the right form?

Then username variable will be used as follow:

"fnServerParams": function (aoData) {
aoData.push({
"name": "owner",
"value": username
})}

Thank you

Answers

  • allanallan Posts: 63,530Questions: 1Answers: 10,473 Site admin

    If you want to send extra information to the server, us the ajax.data option if you are already using ajax.

    Allan

  • rainolfrainolf Posts: 56Questions: 6Answers: 0

    Not what i want.....i need to get data from php server page and retrieve a variable that can be used later in the datatables where clause.

    /*
    * Editor client script for DB table addressbook
    * Automatically generated by http://editor.datatables.net/generator
    */
    $.ajax({
    url: "http://localhost/Web%20Projects/Conference%20Platform/include/pages/sessions.php",
    dataType: 'json',
    cache: false,
    success: function(data) {
    var owner = data.owner;
    session = data.session;
    //console.log(session.user_id); // deal with session
    var username = session.toLowerCase();
    }
    });

    (function($){

    $(document).ready(function() {
    var editor = new $.fn.dataTable.Editor( {
    "ajax": "php/table.addressbook.php",
    "table": "#addressbook",
    "fields": [
    {
    "label": "CONTACT NAME",
    "name": "name",
    "type": "text"
    },
    {
    "label": "COMPANY",
    "name": "company",
    "type": "text"
    },
    {
    "label": "CONTACT EMAIL",
    "name": "email",
    "type": "text"
    },
    {
    "label": "DESCRIPTION",
    "name": "description",
    "type": "text"
    },
    {
    "label": "OWNER",
    "name": "owner",
    "type": "text"
    },
    ]
    } );

    $('#addressbook').dataTable( {
        "sDom": "Tfrtip",
        "sAjaxSource": "php/table.addressbook.php",
        "aoColumns": [
            {
                "mData": "name"
            },
            {
                "mData": "company"
            },
            {
                "mData": "email"
            },
            {
                "mData": "description"
            },
            {
                "mData": "owner"
            }
        ],
        "oTableTools": {
            "sRowSelect": "multi",
            "aButtons": [
                { "sExtends": "editor_create", "editor": editor },
                { "sExtends": "editor_edit",   "editor": editor },
                { "sExtends": "editor_remove", "editor": editor }
            ]
        },
    
    
        "fnServerParams": function (aoData) {
            aoData.push({
                "name": "owner",
                "value": username
            })}
    
    } );
    

    } );

    }(jQuery));

    As you can see the first part will retrieve username variable that should be passed to fnServerParams to filter the query (where username=xxxx).

    Right now the username has seen as undefined cause the variable isn't visible to datatables.
    So i'm asking if i can do the same inside DataTables script in order to let DataTables to recognize it.

    Thank you

  • allanallan Posts: 63,530Questions: 1Answers: 10,473 Site admin

    Why do you say ajax.data isn't what you want? It looks to me it is exactly what you want.

    $('#example').dataTable( {
       "ajax": {
           "url": "data.json",
           "type": "POST",
           "data": function ( d ) {
              d.owner = username;
           }
        }
    } );
    

    Allan

  • rainolfrainolf Posts: 56Questions: 6Answers: 0

    Maybe i'm not so familiar yet....so i'm having some difficulties to integrate this code.

    Basically i need to retrieve some extra parameter from another php page.

    So this is the way:

    sessions.php -> retrieve username variable
    table.addressbook.js -> create DataTable with data from server side script called table.addressbook.php and passing username retrieved from sessions.php with "fnServerParams" that will build the WHERE clause in the DataTables Editor query.

    Hope it will be clear.

    Thank you

  • rainolfrainolf Posts: 56Questions: 6Answers: 0

    I've finally solved the problem and now i'm able to pass the correct values.

    Thank you to pointing me to the right way.

    Anyway,I'm facing in a strange behavior when pass parameter:
    This is the working code:

    "fnServerParams": function (aoData) {

            alert(username);
        aoData.push({
            "name": "owner",
            "value": username
        })} 
    

    And this is the non working one:

    "fnServerParams": function (aoData) {
    aoData.push({
    "name": "owner",
    "value": username
    })}

    It seems that fnServerParams are not able to pass data in time and the table results empty while if a wait some seconds (for example with an alert command) the table is correctly populated.

    What do you think about?

    Thank you

This discussion has been closed.