Jump to content

PHP5, JS, XML and ajax is good for hair loss!


GURAQTINVU

Recommended Posts

OK, I've tried solo, but as it's not my day job, I'm now asking (N.B. This really is a php question I think, honestly).

 

I have an Ajax test project below that firstly attaches a functon to 'on-click' events - successfully (in principle).

When a link is clicked, the 'on-click' function uses XMLHttpRequest (via client Javascript) to try to open that PHP file (on the server), which through a 'case' construct choses the correct XML file, which it SHOULD pass back, which should (via client Javascript) parse as required.

 

OK, it doesn't.  The problem seems to be in the use of the intermediate PHP file - because it all works if the XML file is hard wired into the 'on-click' function.  I'm sure the main problem is in the headers that are sent or recieved (or not) but this is not my forte and I haven't been able to find anyone who is using this same route, which aims to degrade gracefully if Javascript is off on the client (I have succesfully implemented the solution of PHP parsing for when it is off).

 

function PrepareLinks() {

if (!document.getElementById || !document.getElementsByTagName) {return;}

if (!document.getElementById("mylist")) {return;}

var list = document.getElementById("mylist");

var links = list.getElementsByTagName("a");

for (var i=0; i<links.length; i++) {

links.onclick = function() {

var queryPageID = this.getAttribute("href").split("?")[1]; //get the value of query string 'page'

var url = "siteCompletePageDetails.php?"+queryPageID;

if (grabFile(url) == true) {return false;} else {return true;}

}

}

}

 

function grabFile(file) {

  var request = getHTTPObject();

  if (request) {

    request.onreadystatechange = function() {

      parseResponse(request);

    };

    request.open("GET", file, true);

    request.send(null);

    return true;

  } else {

    return false;

  }

}

 

function parseResponse(request) {

  if (request.readyState == 4) {

    if (request.status == 200 || request.status == 304) {

      var data = request.responseXML;

.

.

.

.

.

.

}

    }

        }

 

//siteCompletePageDetails.php

<?php

if (isset($_GET["page"])) { //Only true when a link is clicked and Javascript is ON, and DOM fully supported in Browser and Div tags available.

switch ($_GET["page"]) {

case "testFile": include "testFile.xml";

break;

}

}

?>

 

Please help if you can - thanks. :-\

Thanks for both suggestions firstly, but have to report back no success.  The responseXML always comes back basically empty, although the string is in responseText - but I think this is not new.  By the by, the 'type' of responseXML that is retuned is IXMLDOMDocument2, which means very little to me.

 

I am trying to avoid parsing simple text as XML is supposed to have done all the hard work in essence.

N.B. Actually, I also originally tested in Firefox to no avail, but for me it is not testimony to much other than I had bugs of one sort or another then too, and that these too may or may not exist in the lastest inevitably tweaked attempt to get it working.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.