How to get php database values before the page has loaded using javascript?

So i have 3 pages. upload.php, main.php and script.js. Upload.php lists some database entries, and has a button behind each entry for more info on the entry. When you press the button it gives the ID value from the database with POST in the url and so it loads the info in main.php with the given id.

So here is the problem, i have a javascript file: script.js which needs those db values. I watched a tutorial on passing a variable from php to js using ajax. But the problem is that the values on main are only posted when you press the button in upload.php. Which means when i run the script it immeadiatly goes to main.php and states that the values are empty. Which is true ofcourse.

How do i make the javascript get the values from main/ or database directly?

Im very new to js.