Retrieve Session from php and use in DataTable as query variable
Retrieve Session from php and use in DataTable as query variable
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
If you want to send extra information to the server, us the
ajax.data
option if you are already usingajax
.Allan
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"
},
]
} );
} );
}(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
Why do you say
ajax.data
isn't what you want? It looks to me it is exactly what you want.Allan
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
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) {
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