Heyy everyone..
well i'm a newbie to the whole web developing world..
was just tryin my hand at a simple ajax-php code..
The problem i'm encountering is that my handleServerResponse() method throws an exception and the whole of my PHP code is written into the exception.Any idea why this has been happening ??
I've also attached a screenshot of the error.
If anyone could help me out with this..it'd be of great help !
Thnks![]()
Code:
main.html
HTML Code:<HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <body> <iframe src="frame_a.html" name="frame1" id="frame1" align="right" width=695 height=500></iframe> <iframe src="frame_b.html" name="frame2" id="frame2" width=150 height=500></iframe> </body> </HTML>
frame_a.html
HTML Code:<HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <h1>Images</h1> <input type="text" name="imgname" id="imgname" value="Image-3" onFocus='blur()'></input> <hr> <img id="img01" src="pagerror.gif" hspace="125" vspace="100"></img> <hr> <p><b>Server Response</b></p> <div id="myDivElement"> </div> </BODY> </HTML>
frame_b.html
HTML Code:<HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <script type="text/javascript"> var xmlHttp=createXMLHttpRequestObject(); function createXMLHttpRequestObject() { var xmlHttptemp; try { // Firefox, Opera 8.0+, Safari xmlHttptemp=new XMLHttpRequest(); return xmlHttptemp; } catch (e) { // Internet Explorer try { xmlHttptemp=new ActiveXObject("Msxml2.XMLHTTP"); return xmlHttptemp; } catch (e) { try { xmlHttptemp=new ActiveXObject("Microsoft.XMLHTTP"); return xmlHttptemp; } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } } function ajaxfunc() { var imgsource=parent.frame1.document.getElementById("img01").src; var imgname=parent.frame1.document.getElementById("imgname").value; var url="Images.php?&img_src_present="+imgsource+"&img_name_present="+imgname; if (xmlHttp) { alert("Connecting to Server..."); // try to connect to the server try { // initiate reading a file from the server xmlHttp.open("GET", url, true); xmlHttp.onreadystatechange = handleRequestStateChange; xmlHttp.send(null); } // display the error in case of failure catch (e) { alert("Can't connect to server:\n" + e.toString()); } } } function handleRequestStateChange() { // when readyState is 4, we are ready to read the server response if (xmlHttp.readyState == 4) { alert(xmlHttp.readyState); // continue only if HTTP status is "OK" try { // do something with the response from the server handleServerResponse(); } catch(e) { // display error message alert("Error reading the response: " + e.toString()); } } } function handleServerResponse() { var xmlResponse = xmlHttp.responseXML; // catching potential errors with IE and Opera if (!xmlResponse || !xmlResponse.documentElement) throw("Invalid XML structure:\n" + xmlHttp.responseText); // catching potential errors with Firefox var rootNodeName = xmlResponse.documentElement.nodeName; if (rootNodeName == "parsererror") throw("Invalid XML structure"); // obtain the XML's document element xmlRoot = xmlResponse.documentElement; var srcData = xmlRoot.getElementsByTagName("Source")[0].text; var nameData = xmlRoot.getElementsByTagName("Name")[0].text; var srcDataArray=xmlRoot.getElementsByTagName("Source"); var nameDataArray=xmlRoot.getElementsByTagName("Name"); parent.frame1.document.getElementById("img01").src=srcData; parent.frame1.document.getElementById("imgname").value=nameData; var html = ""; // iterate through the arrays and create an HTML structure for (var i=0; i<srcDataArray.length; i++) html += srcDataArray.item(i).firstChild.data + ", " + nameDataArray.item(i).firstChild.data + "<br/>"; // obtain a reference to the <div> element on the page myDiv = document.getElementById("myDivElement"); // display the HTML output myDiv.innerHTML = html; } </script> </HEAD> <BODY> <form name="formB" onSubmit="ajaxfunc()"> <input type="submit" value="Change"> </form> </BODY> </HTML>
Images.php
PHP Code:<?php
function sendXMLResponse($imgnxt,$nmnext)
{
header('Content-Type: text/xml');
$dom = new DOMDocument();
$response = $dom->createElement('Response');
$dom->appendChild($response);
$imgs = $dom->createElement('Image');
$response->appendChild($imgs);
$source = $dom->createElement('Source');
$sourceText = $dom->createTextNode($imgnxt);
$source->appendChild($sourceText);
$name = $dom->createElement('Name');
$nameText = $dom->createTextNode($nmnext);
$name->appendChild($nameText);
$imgs->appendChild($source);
$imgs->appendChild($name);
$xmlString = $dom->saveXML();
echo $xmlString;
}
$img=$_GET['img_src_present'];
$imgname=$_GET['img_name_present'];
$i=0;
$imgarr[0]="file:///C:/Inetpub/wwwroot/help.gif";
$imgarr[1]="file:///C:/Inetpub/wwwroot/mmc.gif";
$imgarr[2]="file:///C:/Inetpub/wwwroot/pagerror.gif";
$imgarr[3]="file:///C:/Inetpub/wwwroot/print.gif";
$imgarr[4]="file:///C:/Inetpub/wwwroot/warning.gif";
$imgarr[5]="file:///C:/Inetpub/wwwroot/web.gif";
$imgarr[6]="file:///C:/Inetpub/wwwroot/winxp.gif"
$imgnm[0]="Image-1";
$imgnm[1]="Image-2";
$imgnm[2]="Image-3";
$imgnm[3]="Image-4";
$imgnm[4]="Image-5";
$imgnm[5]="Image-6";
$imgnm[6]="Image-7";
for($i=0;$i<6 ;$i++)
{
if(!strcmp($img,$imgarr[i]) && !strcmp($imgname,$imgnm[i]))
{
$imgnext=sprintf("%s",$imgarr[i+1]);
$imgnmnext=sprintf("%s",$imgnm[i+1]);
}
}
if(!strcmp($img,$imgarr[6]) && !strcmp($imgname,$imgnm[6]))
{
$imgnext=sprintf("%s",$imgarr[0]);
$imgnmnext=sprintf("%s",$imgnm[0]);
}
sendXMLResponse($imgnext,$imgnmnext);
?>


Reply With Quote

Bookmarks