Skip to content
Advertisement

How to get MySQL data from PHP to Javascript via XMLHttpRequest and output the data chronologically?

I’ve managed to get the MySQL data that I needed in order for me to display it onto the notification’s container. Now I have a problem at manipulating the data I needed. Instead of printing the objects one by one, it prints the same object at multiple times. Here is the source code.

PHP MySQL data getter

JavaScript

Javascript that gets the value of MySQL data from PHP file. Don’t mind the long string, it’s just an element that will be added up to the notification container when scrolled down.

JavaScript

Html file

JavaScript

Though when I printed the objects into the console, it has the organized data.

Advertisement

Answer

This is because when you get any fetch response the for loop has done iterating so index (inside the testing function) always has the same value. You need to pass index has parameter of testing function.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement