Difficulty linking JS & Server Side Processing PHP
Difficulty linking JS & Server Side Processing PHP
Hi there, I am using DataTables for a project I'm working on and got my DataTables working just fine, but when I remove the JS code from the footer.php include and put it in its own JS file (I will have multiple DataTables so it will be cleaner for the JS to be in its own file instead of in footer.php) everything breaks. Any thought why?
Here is my JS code:
<script>
$(document).ready(function() {
$('#aging_projects').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "../inc/aging-projects-server-processing.php"
} );
} );
</script>
I placed that code in a JS directory in its own file and included the script tag in the footer. The footer.php is in a separate INC directory along with the aging-projects-server-processing.php and ssp.class.php. Why is my data not displaying? When I simply place the script in the footer and change the path to aging-projects-server-processing.php it works just fine.
Thank you for your help!
This question has an accepted answers - jump to answer
Answers
We would need a link to the page showing the issue (per the forum rules ) to understand why it isn't working. What you have done sounds fine on first reading, but obviously something isn't right.
Allan
Currently, my instance of DataTables is only on a local project. Could I provide a link to a GitHub repo instead?
If I can download it and run it in a browser without needing to configure things or set up a special environment - sure!
Allan
Great - please use this repository as a reference:
* https://github.com/erbanach/data-tables
Thank you!
Can you give me full details of what I need to do in order to recreate the issue please? Obviously download the repo, then what?
Allan
Move the Javascript from footer.php to a new file in JS and change the path to aging-projects-server-processing.php. If you navigate to tm-data.php, it breaks the data table. I'm wondering why this is.
Thanks! i'll get back to you on this first thing tomorrow morning.
Allan
Thank you!
After fiddling around to get the database connection working and creating a table to match your own, all I did was remove the code from the
footer.php
file and add:where the DataTables initialisation was before.
And then put the DataTables initialisation into
js/data-table.js
. Reload the page and that works. Note that theajax
URL wasn't changed since the URL is relative to the page where it was loaded, not where the Javascript file is.If that doesn't do the trick for you, there will probably be an error message shown in your browser's console, or flashed up as an Alert. What is that error?
Regards,
Allan
That worked - thank you so much! I have been really impressed with your support forum responses - and I'm very happy to be a supporter of DataTables