AJAX调用和运行PHP脚本

时间:2010-10-13 17:11:37

标签: php javascript ajax

我的服务器上有一个可用的PHP脚本,还有一个带有JavaScript和AJAX的HTML页面,我想调用它并运行PHP脚本。但是,AJAX responseText显示所有PHP代码而不是运行它。我需要做什么才能获得PHP的结果?我看过的其他例子使用了responseText,它看起来效果不错,但不适合我:(

谢谢,

elshae

我的AJAX代码如下......我的PHP工作正常,已经过测试:)

    function ahah(url) {
               //document.getElementById(target).innerHTML = ' Fetching data...';
               if (window.XMLHttpRequest) {
                 req = new XMLHttpRequest();
               } else if (window.ActiveXObject) {
                 req = new ActiveXObject("Microsoft.XMLHTTP");
               }
               if (req != undefined) {
                 req.onreadystatechange = function() {ahahDone(url);};
                 req.open("GET", url, true);
                 req.send("");
               }
             }  

             function ahahDone(url) {
               if (req.readyState == 4) { // only if req is "loaded"
                 if (req.status == 200) { // only if "OK"
                  var div = document.createElement('DIV');
                  div.innerHTML = req.responseText;
                  document.getElementById('chicken_contentDiv').appendChild(div);
                 } else {
                   " <div> AHAH Error:\n"+ req.status + "\n" +req.statusText + "</div>";
                 }
               }
             }

             function load(name) {
              ahah(name);
              return false;
              }
<div> + load('./getFiles.php') + </div> //called in a div

好的,这是新代码:

//这里发生了一些事情,IMO认为这与这个问题无关......

//This is where the AJAX/JQuery calls the php
var info = new OpenLayers.Control.WMSGetFeatureInfo({
                    url: 'http://localhost:8080/geoserver/wms',
                    title: 'Identify features by clicking',
                    queryVisible: true,
                    eventListeners: {
                        getfeatureinfo: function(event){              
                           map.addPopup( new OpenLayers.Popup.AnchoredBubble(
                                "chicken",
                                map.getLonLatFromPixel(event.xy),
                                null,
                                event.text + '<div> Hello Tibet :)</div>' + $('#chicken_contentDiv').load('http://localhost/mapScripts/getFiles.php'), //have also tried localhost:80, no diff

                                null,
                                true

                            ));

                        }

                     }
                });
                map.addControl(info);
                info.activate();

    });

3 个答案:

答案 0 :(得分:0)

如果响应包含实际的PHP代码,那么PHP解释器不会处理它。你在哪里运行这个?很明显,Web服务器没有正确配置来处理PHP文件。

编辑:

你有这条线:

event.text + '<div> Hello Tibet :)</div>' + $('#chicken_contentDiv').load('http://localhost/mapScripts/getFiles.php'),

不正确..你不想附加jQuery函数的结果。输出始终是一个对象。您只想运行该脚本,该脚本将填充ID为chicken_contentDiv的DIV。 (这是真正适合放入细节的DIV吗?)

在var info声明关闭并完成后,它应该在最后。

答案 1 :(得分:0)

在apache config或.htaccess文件中添加此行AddType application/x-httpd-php .html,以便使用php解释器解析html文件。

答案 2 :(得分:0)

你是否错过了getFiles.php文件开头的&lt;?php?