www.webdeveloper.com
Results 1 to 6 of 6

Thread: Trouble with XMLResponse from PHP.

  1. #1
    Join Date
    Nov 2007
    Posts
    6

    Trouble with XMLResponse from PHP.

    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">&nbsp;</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<;$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);
      
    ?>
    Attached Images Attached Images

  2. #2
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    does your server support PHP?

  3. #3
    Join Date
    Nov 2007
    Posts
    6
    I've actually been using the IIS server...i'm sure i've configured it right to support PHP. Do you think it could be coz of this ? I restarted the Server too..i still get the same problem.

  4. #4
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    your getting this error because the file is simply just not being detected and thus processed as PHP, there was a rare bug that could cause this on some apache configs, but the likely hood of you encountering it is far lower(by about a factor of 5,000) than you configuring it incorrectly.

  5. #5
    Join Date
    Nov 2007
    Posts
    6
    Okk i tried my hand at configuring the Apache server..
    This gives me a syntax error on the line where i provide the path for loading php module in the httpd.conf file...
    i copied the php5apache2.dll file into the 'modules' directory under the apache installation directory...
    yet it says unable to load the module when i try to restart the Apache server..

    i donno how its encountering a syntax error...i even use the fwd slashes...
    Scragar cud u help me with this...Thnx man !

  6. #6
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    I have never actually manually configured apache under anything other than a home Linux box, so I can't be of any help there.

    sorry 'bout that.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles