Difficulty linking JS & Server Side Processing PHP

Difficulty linking JS & Server Side Processing PHP

erbanacherbanach Posts: 18Questions: 5Answers: 0
edited August 2016 in Free community support

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

  • allanallan Posts: 63,352Questions: 1Answers: 10,444 Site admin

    Why is my data not displaying?

    We would need a link to the page showing the issue (per the forum rules :smile:) to understand why it isn't working. What you have done sounds fine on first reading, but obviously something isn't right.

    Allan

  • erbanacherbanach Posts: 18Questions: 5Answers: 0

    Currently, my instance of DataTables is only on a local project. Could I provide a link to a GitHub repo instead?

  • allanallan Posts: 63,352Questions: 1Answers: 10,444 Site admin

    If I can download it and run it in a browser without needing to configure things or set up a special environment - sure!

    Allan

  • erbanacherbanach Posts: 18Questions: 5Answers: 0

    Great - please use this repository as a reference:
    * https://github.com/erbanach/data-tables

    Thank you!

  • allanallan Posts: 63,352Questions: 1Answers: 10,444 Site admin

    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

  • erbanacherbanach Posts: 18Questions: 5Answers: 0

    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.

  • allanallan Posts: 63,352Questions: 1Answers: 10,444 Site admin

    Thanks! i'll get back to you on this first thing tomorrow morning.

    Allan

  • erbanacherbanach Posts: 18Questions: 5Answers: 0

    Thank you!

  • allanallan Posts: 63,352Questions: 1Answers: 10,444 Site admin
    Answer ✓

    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:

    <script src="js/data-table.js"></script>
    

    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 the ajax 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

  • erbanacherbanach Posts: 18Questions: 5Answers: 0

    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 :)

This discussion has been closed.