dongxun6690 2012-04-11 00:23
浏览 56
已采纳

使用AJAX从Javascript运行整个PHP文件

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?

  • 写回答

2条回答 默认 最新

  • douxian9943 2012-04-11 01:16
    关注

    I think you're looking for jQuery.getScript

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?