I'm new to PHP and I'm trying to accomplish something relatively simple. I'm trying to get PHP to read data from MySQL and plot it on a web interface. The code in the main script (index.php) queries MySQL and reads two numerical arrays into two php variables $x and $y. Once the data is loaded, it can be plotted on index.php using AJAX. The user can click on a button (html ) where the onclick event uses AJAX to load up another php script that will perform the required plot. I'm obliged to add the plotting code (for several types of plots) into separate files and not as functions in index.php because AJAX and the onclick event can only load up another file on the server (e.g. a php file) and not a php function in the same file where the variables are (index.php). As a result, the php scripts that do the plotting do not have access to variables $x and $y since they were declared in index.php. I'm looking for a way to make variables $x, $y (declared in index.php) accessible from the different plotting scripts called by AJAX.
I tried declaring as static and global but they ($x and $y) remain unseen in the plotting scripts. Obviously I can't import index.php in the plotting scripts nor can I write and read $x and $y from files. I tried using a JavaScript function that would use document.write() to include the plotting code at onclick, long story short it didn't work either and I haven't been able to find a solution on the internet.
<?php
//loading data from mysql
$x = ... //$x and $y are declared here
$y = ...
//basicplot.php is unable to access $x and $y
echo "<button type=\"button\" onclick=\"loadXMLDoc('basicplot.php', 'mydiv')\">Plot data</button>";
echo "<div id=\"mydiv\"><br>[plot goes here]</div>";
?>
NB: Plotting does work when I copy the code from basicplot.php to index.php but since I want to have many types of plots that's not really an option.