Populate a DataTable with data from a MySQL database

Actually, it doesn’t. Here’s why:

The folder structure of my project is as follows:

First is the CCRP folder where the entire project is located (what I call the root folder).

C:\xampp\htdocs\ccrp

After that, I have the following:

Inside my api folder is the server.php file and the ssp.class.php file. Inside my js folder is the datatables.js file and other js files that the template requires. When you mention that I’m telling it to go one folder up into a folder called api to find server.php, it should be going back to the root folder (ccrp) then into api, followed by server.php. I think it is, but for some reason, it isn’t pulling in the data.

In your datatables.js change this:

"ajax": "../api/server.php"

into this:

"ajax": "api/server.php"

And try and tell me if you succeeded.
Browser pulls datatables.js out from js folder and sets it as it is in the root, so after that it is easy and that is why you have to remove the two dots.

That didn’t work. It wouldn’t work anyway because that file is not in the root folder, it is in the js folder. By removing the two dots, you’re telling the js file to look into a folder called api in the current directory and look for a file called server.php and both of them do not exist in the js directory. That’s why you need the two dots in there because you’re telling the js file to go back one folder, then look for the api folder where the server.php file is. The problem is that it says “No data available in the table.”

Actually it works.

No, it’s not a problem, you actually have no data in your SQL table, your table is empty. Hence DataTables says “No data available in the table.”

No it doesn’t, otherwise I wouldn’t have said that. I just tried this moments ago.

Yes, it is a problem and my table is not empty. This has been verified by various people in this post. I even posted a screenshot in this post that shows the data being called by the PHP file, but I had to remove it because it is personal data that I cannot show to the public. @droopsnoot can even vouch for this:

1 Like

Indeed, I saw a screen shot showing data when the PHP code was executed directly. So either the PHP is not being executed, or the results are not being passed in to the table correctly.

But, have you tried writing a bit of text to a file using file_put_contents() as part of your PHP, just to see if the file gets created? If it does, you know your PHP is being run. If not, it isn’t.

I did try this and the phptest.html file was created:

However, I had to run the file directly. I think you are right. Something is not communicating with the PHP file, but I do not know what it could be.

When I load my browser console, I also do not see the PHP file in this list, but datatables.js is visible:

I think I found something:

I went back and double checked my js file to make sure it was loading correctly. When I checked the code in my browser to see what it was calling, it looked like this:

$(document).ready(function() {
    $('#dataTable').DataTable();
});

I refreshed the page and the code changed to this:

$(document).ready(function() {
    $('#dataTable').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "../api/server.php" // your php file
    });
});

Now I see this:

According to the browser console, it’s showing me that my PHP file is returning a 404. Why would that be happening?

.. means “go up a folder.”
You are at /ccrp/
Your script is telling it to contact /api/server.php.
Your file is at /ccrp/api/server.php.
It 404’s because you’re telling it to find the file in the wrong place.
Remove the ../ from the beginning of the ajax directive.

1 Like

That did it!! THANK YOU! It has taken me over a week to figure this out!

Now I need to figure out how to add and delete records.

Didn’t I already give you the answer in my #23 post?. @jmyrtle, you obviously don’t read.

I actually did read this: Your post was this:

This is false. My table was never empty to begin with as I previously stated. When I tried what you suggested, it didn’t work at first because the code had never refreshed and I did not know this. When I loaded the js file directly and refreshed the page, the code refreshed to the code I had typed and provided the error. It was only then that I followed yours and @m_hutley’s guidance to solve the problem.

So yes, you partially provided the answer. And for that, I apologize.

1 Like

I came upon your problem, I think it will help you to understand better if you go to this site https://www.datatables.net/ there should be an equivalent way to do this in PHP; it just a web page isn’t it and jQuery works in conjunction with the DOM no mater which website, at least that’s jQuery’s raison d’etre (reason for being).
Mel

I was already able to solve this problem. Thanks, though!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.