2011-03-17 162 views
1

我是AJAX的新手。任务是我必须从php文件获取数据并将其存储在JavaScript变量中。我经历了许多例子,但没有找到有用的东西。 我在这里给人一种假的html代码:AJAX-从php获取数据

<html> 
<head> 
<script> 
function ajaxfunction() 
{ 
    //code for httprequest 
    **call the php file declare a variable and store the response of php** 
    //return the variable 
} 
</script> 
</head> 
<body> 
    //my code for displaying a map 
    **mainvariable=ajaxfunction();//storing the value of subvariable(data from php) in mainvariable** 
    //use the mainvariable and do the remaining task 
<body> 

我的PHP代码:

<?php 
    $file=fopen("datapoints.txt","r"); 
    $read=fread($file,filesize("datapoints.txt")); 
    fclose($file); 
    echo $read; 
?> 

这里的问题是我没有任何形式的变量在我的HTML文件中使用,同时呼吁PHP文件。简单地当页面加载时,应该调用“ajaxfunction()”并从php获取数据并存储在变量中................

我想你可以理解我的问题

任何帮助是极大的赞赏

+0

@jakenoble的答案是正确的。 '.load()'或'.get()'用于显示内容,'.getJSON()'用于获取数据并存储为jQuery变量。 http://en.wikipedia.org/wiki/JSON – CallMeLaNN 2011-03-18 02:04:19

+0

根据您的问题,您为什么需要Ajax?您可以在html文件中添加php标签,或者将文件重命名为.php。因此,只需简单地填充这个形式的php变量,就像这样:”/>' – CallMeLaNN 2011-03-18 02:06:58

回答

0

如果你想想从PHP发送数据的JavaScript下来,你可以使用json_encode。 要用PHP接收数据,你可以使用$ _GET和$ _POST(只要你正在编写一个简单的应用程序):)

对于你的Ajax请求你(我允许这只是作者这个问题),可以使用我的javascript代码:

function getRequestObject(){ 
    var o = null; 
    if(window.XMLHttpRequest){ 
     o = new XMLHttpRequest(); 
    }else if(window.ActiveXObject){ 
     try{ 
      o = new ActiveXObject('Msxml2.XMLHTTP'); 
     }catch(e1){ 
      try{ 
       o = new ActiveXObject('Microsoft.XMLHTTP'); 
      }catch(e2){ 

      } 
     } 
    } 
    return o; 
} 
function request(method, adress,sendData,callback){ 
    var o = getRequestObject(); 
    var async = (callback!==null); 
    if(method === 'GET'){ 
     if(sendData!=null){adress+="?"+sendData;} 
     o.open(method, adress, async); 
     o.send(null); 
    }else if(method === 'POST'){ 
     o.open(method, adress, async); 
     o.setRequestHeader('Content-Type' , 'application/x-www-form-urlencoded'); 
     o.send(sendData); 
    } 
    if(async){ 
     o.onreadystatechange = function(){ 
      if(o.readyState==4&&o.status==200){ 
       callback(o.responseText); 
      }else if(o.readyState==4&&o.status!=200){ 
       //Error 
      } 
     }; 
    } 
    if(async){return ;} 
    else{return o.responseText;} 
} 

的RequestCache作为对象实现(看看这是如何在Javascript中完成) 但也许JQuery的左右可以解决你的任务了。

function RequestCache(){} 
RequestCache.cache = Array(); 
RequestCache.getRequest=function (method, adress,sendData,callback,enforceReload){ 
    if(enforceReload===null){enforceReload=false;} 
    var url = method+adress+sendData; 
    var newUrl = true; 
    if(typeof(enforceReload)==="undefined"||enforceReload===false){ 
     for(var key in RequestCache.cache){ 
      if(key===url){ 
       newUrl=false; 
       break; 
      } 
     } 
    } 
    if(newUrl){ 
     if(callback){ 
      request(method, adress,sendData, 
       function(res){ 
        RequestCache.cache[url]=res; 
        callback(res); 
       } 
      ); 
     }else{ 
      RequestCache.cache[url]=request(method, adress,sendData,null); 
      return RequestCache.cache[url]; 
     } 
    }else{ 
     if(callback){ 
      callback(RequestCache.cache[url]); 
     }else{ 
      return RequestCache.cache[url]; 
     } 
    } 
}; 
RequestCache.setRequest = function (method, adress,sendData,result){ 
    var url = method+adress+sendData; 
    RequestCache.cache[url] = result; 
}; 
0

做你想做的,我知道什么是最简单的方法,将是jQuery的.load()功能或者是将其存储在一个变量.get()功能。

4

你可以在这里很好的使用jQuery。文档在这里http://api.jquery.com/jQuery.ajax/

下面是一个例子:

<html> 
<head> 
<!-- Include jquery from Google here --> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> 

<script type="text/javascript"> 
// Wait till dom is loaded 
$(document).ready(function() { 
    // When id with Action is clicked 
    $("#Action").click(function() 
    { 
    // Load ajax.php as JSON and assign to the data variable 
    $.getJSON('ajax.php', function(data) { 
     // set the html content of the id myThing to the value contained in data 
     $("#myThing").html(data.value); 
    }); 
    }); 
}); 
</script> 
</head> 
<body> 
    <a id="Action">Click Me</a> 
    <p id="myThing"></p> 
</body> 
</html> 

你ajax.php文件可以只包含:

<?php 
    echo json_encode(array("value" => "Hello World")); 
?>