title: Load PHP-file including Google Chart using jQuery: Chart never shows up!
body: <p>I already created a PHP file that shows me some calculated values as well as a visualization of this data using Google Chart. If I just directly open this PHP everything works fine.</p>
  
  <p>So my next step is to load this PHP(including the google Chart) into another Page when clicking a button by using jQuery. The issue I have is that the calculated values show up without problems but the chart doesn't. Using Firebug I found out that there has to be a problem with the loading of google's API which happens inside the PHP-file (which is needed for the chart visualization). I already did various attempts to load the API inside the page that calls the PHP-file (or even using jquerys getscript) but without success.</p>
  
  <p>Has anybody got an idea what I am missing here, or how i could get this chart to show up?</p>
  
  <p>Thanks a lot for your help!</p>
  
  <hr>
  
  <p>Here is the PHP-file (without the PHP-part):</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Analyseergebnis&lt;/title&gt;
      &lt;!--Load the AJAX API--&gt;
      &lt;!-- +++++++++++++++++++   Definition von Google Charts   +++++++++++++++++++++ --&gt;
      &lt;script type="text/javascript" src="https://www.google.com/jsapi"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div style='width:300px; text-align:center; background-color:lightgrey; padding-top:5px; padding-bottom:5px;'&gt;&lt;b&gt;A N A L Y S E E R G E B N I S&lt;/b&gt;&lt;/div&gt;
      &lt;p /&gt;
  
  
  &lt;!-- +++++++++++++++++++   HERES THE PHP PART - NOT SHOWN HERE  +++++++++++++++++++++ --&gt;
  
      .....................
  
  &lt;!-- +++++++++++++++++++   Aufruf des Diagramms   +++++++++++++++++++++ --&gt;
  
      &lt;script type="text/javascript"&gt;
  
          // Load the Visualization API and the piechart package.
          google.load('visualization', '1', {'packages':['corechart']});
  
          // Set a callback to run when the Google Visualization API is loaded.
          google.setOnLoadCallback(drawChart);
  
          // Callback that creates and populates a data table, 
          // instantiates the pie chart, passes in the data and
          // draws it.
          function drawChart() {
  
              // Create our data table.
              var data = new google.visualization.DataTable();
              data.addColumn('string', 'Topping');
              data.addColumn('number', 'Slices');
              data.addRows([
                  ['Wiesen', &lt;?php echo str_replace(',', '.', $row_wiesen[0]); ?&gt;],   // Konversion da Google Charts als ',' nur '.' akzeptiert
                  ['Acker', &lt;?php echo str_replace(',', '.', $row_acker[0]); ?&gt;],
                  ['versiegelt', &lt;?php echo str_replace(',', '.', $row_versieg[0]); ?&gt;],
                  ['Laubwald', &lt;?php echo str_replace(',', '.', $row_lwald[0]); ?&gt;],
                  ['Nadelwald', &lt;?php echo str_replace(',', '.', $row_nwald[0]); ?&gt;],
                  ['Wasser', &lt;?php echo str_replace(',', '.', $row_wasser[0]); ?&gt;],
                  ['Unklassifiziert', &lt;?php echo str_replace(',', '.', $row_na[0]); ?&gt;]
              ]);
  
              // Instantiate and draw our chart, passing in some options.
              var chart = new google.visualization.PieChart(document.getElementById('gchart'));
              chart.draw(data, {width: 300, height: 200, colors:['#006400','#CD853F','#C0C0C0','#228B22','#556B2F','#1E90FF','#000000'], legend: 'right',  chartArea:{width:"90%", height:"85%"}, pieSliceText:"none"});
          }
      &lt;/script&gt;
  
  
  &lt;div id="gchart"&gt;&lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And here is where I call the PHP-file:</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
   &lt;head&gt;
  &lt;meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" /&gt;
   &lt;meta name="apple-mobile-web-app-capable" content="yes" /&gt;
   &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
  &lt;style type="text/css"&gt;
      #map {
          width: 800px;
          height: 475px;
          border: 1px solid grey;
      }
   &lt;/style&gt;
   &lt;script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&amp;mkt=en-us"&gt;&lt;/script&gt;
  &lt;script src="http://openlayers.org/api/OpenLayers.js"&gt;&lt;/script&gt;
  &lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
  
  ++++++++++ HERE COMES THE OPENLAYERSPART - NOT SHOWN HERE +++++++++++
   &lt;/head&gt;
  
    &lt;body onload="initmap()"&gt;
  
  &lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;div id="map"&gt;&lt;/div&gt;
          &lt;/td&gt;
          &lt;td valign="top"&gt;
              &lt;div id="div1" style="display:visible;"&gt;
              &lt;button id="ajaxloadlink" type="submit"&gt;A N A L Y S E starten&lt;/button&gt;
              &lt;/div&gt;
  
              &lt;div id="ajaxcontent" style="font-family:Verdana; font-size:9pt"&gt;
                  - Bitte zuerst Suchpolygone erfassen! -
              &lt;/div&gt;
              &lt;div id="div2" style="display:none; text-align:right;"&gt;
                  &lt;input type="button" id="ajaxbacklink" value="NEUE Analyse starten" onclick="clearLayerVector()"&gt;
              &lt;/div&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  
  &lt;script type="text/javascript"&gt;
      $.ajaxSetup ({  
          cache: false        // Browser soll den AJAX-Befehl nicht cachen, da PHP-file mehrmals hintereinander abgerufen wird
      });
      $.toggle = function(query)
      {
              $(query).toggle("fast");
      }
      $("#ajaxloadlink").click(function(){
          $.toggle('#div1');
          $("#ajaxcontent").empty().html('&lt;div style="width:300px; text-align:center;"&gt;&lt;p&gt;&lt;img src="../web/loader.gif" /&gt;&lt;/p&gt;&lt;/div&gt;');
          $("#ajaxcontent").load("../web/abfrage.php");
          $.toggle('#div2');
      });
      $("#ajaxbacklink").click(function(){
          //document.getElementById('ajaxcontent').style.display   = "none";
          $("#ajaxcontent").empty().html('&lt;div id="ajaxcontent"&gt;- Bitte zuerst Suchpolygone erfassen! -&lt;/div&gt;');
          $.toggle('#div2');
          $.toggle('#div1');
      });
  &lt;/script&gt;
  
   &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Thanks a lot for your help!</p>
  
  <hr>
  
  <p>Thanks for your advice. The only thing is I am not that big of an jQuery programmer and I am not really sure what's the escence of the post you linked to.
  I ran some more test with a very simplified version only showing the google chart when hitting a button (code is shown below) but without success. It still seems that the JScript code isn't recognized. The only thing I noticed is that is essential that the jquery-code that has the functions in it has to be AFTER the definition of the -tag in the source code.</p>
  
  <p>Here come the two simple files trying to load the chart (I guess it should be easy to reproduce the error I get):</p>
  
  <p>gchart.htm - That's the file that will be called by jQuery - if you run ONLY this file on its own you'll see that the chart is shown without problems:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Testwiese&lt;/title&gt;
      &lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="https://www.google.com/jsapi"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="gchart.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
      // Load the Visualization API and the piechart package.
          google.load('visualization', '1', {'packages':['corechart']});
  
          // Set a callback to run when the Google Visualization API is loaded.
          google.setOnLoadCallback(drawChart);
  
          // Callback that creates and populates a data table, 
          // instantiates the pie chart, passes in the data and
          // draws it.
          function drawChart() {
  
              // Create our data table.
              var data = new google.visualization.DataTable();
              data.addColumn('string', 'Topping');
              data.addColumn('number', 'Slices');
              data.addRows([
                  ['Wiesen', 2],
                  ['Acker', 3],
                  ['versiegelt', 6],
                  ['versiegelt', 6]
              ]);
  
              // Instantiate and draw our chart, passing in some options.
              var chart = new google.visualization.PieChart(document.getElementById('gchart'));
              chart.draw(data, {width: 300, height: 200, colors:['#006400','#CD853F','#C0C0C0','#228B22','#556B2F','#1E90FF','#000000'], legend: 'right',  chartArea:{width:"90%", height:"85%"}, backgroundColor: 'white', pieSliceText:"none"});
          }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div id="gchart"&gt;&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and gchart_test.htm - the file with the button that calls the file above:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="https://www.google.com/jsapi"&gt;&lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
      &lt;input type="button" id="gshow" value="Test Gchart"&gt;
  
      &lt;div id="gchart"&gt;&lt;/div&gt;
  
      &lt;script type="text/javascript"&gt;
          //  load() functions
          var loadUrl = "gchart.htm";  
          $("#gshow").click(function(){  
          $("#gchart").load(loadUrl);
          });
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I am sorry but at this point I have no clue what to read out of the link you send me or what to change in my files. If you run these two files you will see that the loading process crashes and weird as it is the page is attempting to contact google...</p>
  
  <p>Might it be a problem that Google Charts itself is obviously using the same technique to overwrite a div-tag in order to display the chart? </p>
  
  <p>The thing is I separate the code in 2 parts FOR PURPOSE. I want to load gchart_test.htm INTO gchart.htm because in my project gchart.htm is only an example of a much bigger site where I want to lead the google chart into. And in gchart.htm there are only 6 lines of code in order to make it as simple as possible for others to see what I am trying to get down. So the code is just a simplification so everybody could reproduce the error I get.</p>