繁体   English   中英

AJAX调用并运行PHP脚本

[英]AJAX To Call and Run PHP Script

我的服务器上有一个工作正常的PHP脚本,一个包含JavaScript和AJAX的HTML页面,我想调用并运行该PHP脚本。 但是,AJAX responseText显示的是所有PHP代码,而不是运行它。 我需要做什么才能仅获得PHP的结果? 我看过的其他示例使用了responseText,看起来似乎效果很好,但对我来说却不是:(

谢谢,

艾尔莎

我的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();

    });

如果响应包含实际的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声明之后,应该在最后。

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

您是否在getFiles.php文件的开头缺少<?php?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM