views:

85

answers:

3

Hello StackOverflow,

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 :(

Thanks,

elshae

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

    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...

//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();

    });
A: 

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.

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

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

Fosco
"If the response contains actual PHP code, then it is not being processed by the PHP interpreter" Yes precisely! I am running this from an html file on my server. Hmm I will look both into configuring my server and jQuery. Is there any advantage to using jQuery as opposed to what I have done??? Thanks!
elshae
You have 1 or 2 issues. Your javascript might work fine, but your web server is not running the PHP script. It doesn't matter that you're calling it from a .HTML file. Take Ajax out of the equation and browse to getFiles.php (and view source... see the code? that's your problem).. Once you fix that, does your javascript work? If not that's problem 2. jQuery makes things like that easy. `$('#divId').load('getFiles.php');` That's it, no other functions.
Fosco
Thanks for the responses. I replaced <div> + load('./getFiles.php') + </div> to $('chicken_contentDiv').load('./getFiles.php'), now my popup is display [object object]...any other suggestions? btw Fosco, I'm a she ;)
elshae
@elshae sorry about that... what popup?
Fosco
I want the result of the php script to end up on a popup...I am almost positive that my issue is something to do with the way my AJAX call is parsing the PHP script :(
elshae
@elshae You said you switched to jQuery?.. Can you edit and add the current code (both HTML and JS) ?
Fosco
@elshae "something to do with the way my AJAX call is parsing the PHP script" - PHP has to be executed by the server. Javascript, which executes on the client, shouldn't be parsing your php.
SorcyCat
@SorcyCat, if my responseText is the raw php code, does this mean that my server is not executing my PHP script?
elshae
@elshae yes, that's what it means.
Fosco
@elshae @Fosco I agree with Fosco
SorcyCat
OK a little embarrassing to admit, but I had my PHP script in the wrong place...However, now that I do have it in the right place (on my server), my responseText is apparently empty? I have some echo statements in there, runs fine in the browser under localhost/myscript.php. What could be wrong?
elshae
@elshae is it the same code as in the question?... try the jQuery method you posted.. $('chicken_contentDiv').load('./getFiles.php');
Fosco
When I use that jQuery method I get an [object object] displayed in the div rather than the contents of the php script...
elshae
@elshae sounds like something is wrong with your syntax.. can you post the code you're using for that?
Fosco
Actually I think I have narrowed the problem down to that my script is on port 80 and my page is on 8080. In firebug's NET tab it is saying that the domain is localhost:8080...is there anyway I can direct the call to the 80 port?
elshae
@elshae why is your page on 8080?
Fosco
I have Geoserver running on port 8080 and my page is in the www directory of Geoserver. Basically I am creating a slippy map and I want the user to be able to click on the map and have a pop-up displayed with content called from a PHP script. This is why my PHP was not being executed, I had my PHP on the Geoserver rather than the Apache server. Now my PHP has been moved to the Apache server where it should have been all along, but I still need my page on the Geoserver...
elshae
@elshae I think your issue is because of the port... see http://en.wikipedia.org/wiki/Same_origin_policy but I'd still like to see the exact code you're running.
Fosco
Yeah it seems my issue is because of the port. Actually I think it is the issue I have been having for quite some time now. It seems my proxyhost is also not found due to this issue of it being on a different port (80).
elshae
Ok I also edited my code. There are a few things I did have wrong, syntax/logic wise, but unfortunately there's still the same issue. I thought the same-origin policy only kicks in if the server is trying to access a remote server. Here in my case the server is trying to access itself on a different port. Unless I am horribly mistaken...
elshae
You can make your own proxy of the geoserver by having a PHP page which gets the results from geoserver on the serverside and echo's them out. Then you can call that page to get the geoserver content.
SorcyCat
@elshae I updated my answer with an additional issue.
Fosco
It's still not working. The responseText of the php file is blank. Seems like it cannot fetch that file due to the different port. Guess I will now have to weigh my options about how I am going to go about setting up a proxy for these two servers. I really appreciate all the help! :) You guys are awesome!
elshae
To anyone using Geoserver on a different port from their Apache config should be directed here on how to set up a proxy for Geoserver: http://geoserver.org/display/GEOS/GeoServer+Proxy+Extension
elshae
A: 

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.

egis
This won't help since he is calling a .php file with Ajax, not including PHP code in a .html file.
Fosco
sorry, i misinterpreted elshaes comment where he says "Yes precisely! I am running this from an html file on my server."
egis
A: 

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

SorcyCat
No and my php script runs great on the command line :)
elshae