0

on $(document).ready(function() in index.php, the below AJAX is executed, running the file getData.php:

if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("dataSuccess").innerHTML=xmlhttp.responseText;
        } 
      }
    xmlhttp.open("GET","getData.php",true);
    xmlhttp.send();

In the getData.php file, data is gathered from MySQL and put in JS-arrays:

  var guestData = new Array("<? for ($j=0;$j< sizeof($guestData);$j++) { print     ($guestData[$j]); ?>","<?php } $j = $j+1; ?>");

And finally, store the data in my js arrays into LocalStorage:

   var guestDataCols = new Array();
    guestDataCols = guestData[0].split(",")


        var arrayIndex=0;
        for(arrayIndex=0;arrayIndex<guestData.length-1;arrayIndex++)
        {
            localStorage.setItem(arrayIndex, guestData[arrayIndex]); // storing
        }

EVERYTHING works! But the problem is that my AJAX code doesn't seem to run through the entire getData.php file since LocalStorage in yet empty after the php-file is executed via AJAX. However (and this is a big one), if I simply refresh getData.php in another window, data is stored perfectly and evernything works. I've also tried using jQuery for this as suggested in another Stack Overflow question

  $('#dataSuccess').load('getData.php'); //instead for the AJAX code 

but with the exact same and somewhat mediocre result. So my questions is, why isn't the AJAX script running the entire php file and HENCE, why is no data stored in LocalStorage?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Jonathan
  • 2,953
  • 3
  • 26
  • 37
  • It looks like you use the response from `getData.php` as the innerHTML for `#dataSuccess`, but where do you actually execute it? – Sean Hogan Apr 11 '12 at 00:45
  • that's partly true. For the moment the respons from getData is simply "Mission completed" to alert that the file has been opened and processed by AJAX. It's irrelevant tough. All i need is a way to run an entire PHP file from javascript (which according to the below answerer is not done with AJAX and XML) – Jonathan Apr 11 '12 at 01:01

2 Answers2

2

JavaScript on an HTML page is not run when called by an XMLHttp request. The browser doesn't parse the pages that JavaScript receives over XMLHttp requests and therefore does not run the JavaScript. You would have to output to the browser for it to be run. Your best bet would be do have the PHP return the data you need and then extract it from the XMLHttp request. For example, the getData.php could return a JSON string containing the data you need. Then on the page with the XMLHttp request, you could parse that JSON string and save it to the localStorage on that page.

Peritia
  • 103
  • 1
  • 4
  • I don't really care about what the PHP returns, all i really want is for the PHP code to be execuded (so data gets stored in localStorage). Isn't there just a way from js to run a complete file without anything being returned? One would think .load('getData.php'); in jQuery would perform such a task but apparently not. Any other ways? – Jonathan Apr 11 '12 at 00:54
0

I think you're looking for jQuery.getScript

Sean Hogan
  • 2,902
  • 24
  • 22