How to Put Table Name In a VARIABLE ??
How to Put Table Name In a VARIABLE ??
daguerfi
Posts: 6Questions: 3Answers: 0
Hi, I want to change the database table name from the client side via an input form. How To post the input value to the server-side?
Client side:
$(document).ready(function() {
var tid = $('#table_id').val();
$('#tb-data').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "server_processing.php",
"type": "POST",
// What to put here? :/ :o
} );
} );
Server side:
// DB table to use
$table = $_POST['table_id'];
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
ajax.data
will let you send parameter to the server.Allan
Thanks Allan, But how to read the data in the server side, I tried both Post and Get parameters with no luck. Is this how to use the data option?
How to read the posted data on the server-side? many thanks.
If your
#table_id
was aninput
element that returns a value, then yes. My guess is that that is actually yourtable
and thus it won't have a value.You've specifically marked it as POST so you would use
$_POST['myVariable']
in PHP.Allan
Thanks Allan, It worked like a charm God Bless.
Please one last question if you have a time, how to reload the table with a new ajax call with the same columns, without getting this error 'Cannot Reinitialise Datatable'. I tried the 'destroy' option but I can't figure it out how to refill the table again.
many thanks.
If you are getting that error, then you aren't destroying the table. You might want to use
$.fn.dataTable.isDataTable()
to check if a table is actually a DataTable or not before destroying it.Allan
Ok, I have managed to get it work, Allan thank you very much.
Here's the entire ajax call code for those who's struggling.
I also was having an issue when sorting numbers, they weren't sorting correctly, then later I figure out that I was storing them as a string in database instead of integers (INT).
Hope It's helps someone.
That's the business! There is actually one other option that is a little more efficient - using
ajax.url.reload()
on an already initialised table and usingajax.data
as a function so it will get the value every time it is submitted:Allan