Written by: Robert Guida, guidaMedia.com, ©2009.
Getting a fully functional script that actually reads in XML, parses it, and then sets element values is not easy to find on the Web. I searched for hours, but was only able to find bits and pieces to put together a function. So if you are looking to have a PHP script create an XML document filled with variables and HTML, then this code is for you.
The parts involved in this example code are a PHP file, "AJAX.php", and an HTML file, "AJAX.htm", with a few JavaScript functions.
There is some coordination between the PHP file, and the JavaScript funciton setElementValues(). The tags that hold the key information in the XML document are "formID" and "formVal". More appropriate names would be elementID and elementValue, since these nodes cooresponde to the elements. The setElementValues() function gets the values of these two nodes, and based on the value for formID, that element in the HTML document will have its innerHTML set to the value found in the formVal node. If element name does not match the formID value, then the value is read, but never set.
O.K. That is a lengthy intro, and if you are like me you just want the code, and to see an example. If you feel like you need some explanation, then you RTFM (Read The Freaking Manual). So let's get to the good stuff... the example. Below is some HTML with a few element tags. Click the "Run AJAX XML Script" and see what happens. After that, down load the script, and open it in your editor.
To:
From:
Subject:
Message:
function get_oXMLHTTP(){
var output = null;
try{// Firefox, Opera 8.0+, Safari
output = new XMLHttpRequest();
}catch(e){ // Internet Explorer
try{
output = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
output = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return output;
}
function getAJAXContent_XML(objHTTP, url){
if (objHTTP != null){
objHTTP.open("POST", url, true);
objHTTP.send(null);
objHTTP.onreadystatechange = function(){
if(objHTTP.readyState == 4){
loadXMLString(objHTTP.responseText);
setElementValues();
}
}
}
}
function loadXMLString(xmlData) {
try { //Internet Explorer
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(xmlData);
}catch(e){ //Firefox et. all
try {
parser = new DOMParser();
xmlDoc = parser.parseFromString(xmlData, "text/xml");
}catch(e){
alert(e.message)
}
}
}
function setElementValues(){
var oRV = xmlDoc.getElementsByTagName("elementValues");
var oV = oRV[0].childNodes;
var formID = "";
var value = "";
for (i = 0; i < oV.length; i++){
oFormID = oV[i].getElementsByTagName("formID");
oFormVal = oV[i].getElementsByTagName("formVal");
formID = URLDecode(oFormID[0].text);
formVal = URLDecode(oFormVal[0].text);
if(document.getElementById(formID)){ document.getElementById(formID).innerHTML = formVal; }
}
}
function URLDecode(val){
var output = "";
output = unescape(val);
output = output.replace(/\+/g, " ");
return output;
}
If you have any questions, please do not hesitate to contact me,
click here. I will do my best to get back to you promptly.
Take care and good luck in your endevours.
Rob, 2009
![]() |
|
© 2006 guidaMedia, LLC |
|
|