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条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog