1

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.

en1
  • 237
  • 4
  • 8
  • Pass your $x and $y values in AJAX requests using GET or POST method, so your plot scripts can know them. – Alyas Feb 17 '14 at 16:33

4 Answers4

2

PHP Code run in one request to the server doesn't have any connection to another request. You either need to do get the data from the DB again or output the variables on index.php and post that data to the second php file when doing the AJAX reques.

kraftner
  • 467
  • 2
  • 19
1

At the most basic way, load basicplot.php?x=$x&y=$y which passes the data in the query string and then in basicplot.php use $_GET['x'] and $_GET['y'] to get the values passed.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1

try to send $x and $y by GET method to the script. And get them in that script by $_GET[].

silk_route11
  • 324
  • 3
  • 17
-4

Define the variables as global :

global $x, $y;

Then they are accessible in include files;

Ganz
  • 187
  • 5
  • The OP explicitly states they are not using include() on the other files. As the other files are not run as part of that request, global will make no difference. – Anigel Feb 17 '14 at 16:32
  • 1
    Don't use globals http://stackoverflow.com/questions/12445972/stop-using-global-in-php – floriank Feb 17 '14 at 16:33