AJAX调用和运行PHP脚本

时间:2022-06-02 00:11:35

I have a working PHP script on my server and a HTML page with JavaScript and AJAX which I would like to call and run the PHP script. However, the AJAX responseText is displaying all the PHP code rather than running it. What do I need to do to only get the results of the PHP? Other examples I looked at used the responseText and it seemed to work out well, but not for me :(

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

Thanks,

谢谢,

elshae

elshae

My AJAX code is below...my PHP works fine, it has been tested :)

我的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

Ok here is the new code:

好的,这是新代码:

//Some stuff happens here, IMO think it's irrelevant to this issue...

//有些事情发生在这里,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 个解决方案

#1


0  

To your apache config or .htaccess file add this line AddType application/x-httpd-php .html so html files will be parsed with php interpreter.

到你的apache配置或.htaccess文件添加这行addType application / x-httpd-php .html所以html文件将用php解释器解析。

#2


0  

Are you missing the <?php at the beginning of your getFiles.php file?

你错过了getFiles.php文件开头的<?php吗?

#3


0  

If the response contains actual PHP code, then it is not being processed by the PHP interpreter. Where are you running this? It is obvious that the web server is not properly configured to process PHP files.

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

EDIT:

编辑:

The line you have:

你有这条线:

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

is incorrect.. you don't want to append the outcome of the jQuery function. The output would always be an object. You just want to run that script, which would populate a DIV with an ID of chicken_contentDiv. (is that really the right DIV to put the details in?)

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

It should be at the end, after your var info declaration is closed and done.

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

#1


0  

To your apache config or .htaccess file add this line AddType application/x-httpd-php .html so html files will be parsed with php interpreter.

到你的apache配置或.htaccess文件添加这行addType application / x-httpd-php .html所以html文件将用php解释器解析。

#2


0  

Are you missing the <?php at the beginning of your getFiles.php file?

你错过了getFiles.php文件开头的<?php吗?

#3


0  

If the response contains actual PHP code, then it is not being processed by the PHP interpreter. Where are you running this? It is obvious that the web server is not properly configured to process PHP files.

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

EDIT:

编辑:

The line you have:

你有这条线:

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

is incorrect.. you don't want to append the outcome of the jQuery function. The output would always be an object. You just want to run that script, which would populate a DIV with an ID of chicken_contentDiv. (is that really the right DIV to put the details in?)

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

It should be at the end, after your var info declaration is closed and done.

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