using option box, data retrive from .xml and display in txtbox?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • perhapscwk
    New Member
    • Sep 2007
    • 123

    using option box, data retrive from .xml and display in txtbox?

    I want to use ajax, by selected the option box, retrieve data from a .xml file and fill in to the textbox.

    below is the html page
    Code:
    <script src="selectcd.js"></script>
    
    <form> 
    Select a CD:
    <select name="cds" onchange="showCD(this.value)">
    <option value="Bob">Bob</option>
    <option value="Bonnie">Bonnie</option>
    </select>
    </form><p>
    
    <input type="text" name="txtSingerName" id="txtSingerName" size=20 class="stdbox" value="<?php print stripslashes($form_txtSingerName); ?>">
    <input type="text" name="txtAlbumName" id="txtAlbumName" size=20 class="stdbox" value="<?php print stripslashes($form_txtAlbumName); ?>">
    </p>
    below is the .js file
    Code:
    var xmlHttp
    
    function showCD(str)
    { 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
     {
     alert ("Browser does not support HTTP Request")
     return
     } 
    var url="getcd.php"
    url=url+"?q="+str
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=stateChanged 
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    }
    
    function stateChanged() 
    { 
     if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
     { 
     document.getElementById("txtSingerName").value=xmlHttp.responseText 
     document.getElementById("txtAlbumName").value=xmlHttp.responseText 
     } 
    }function GetXmlHttpObject()
    {
    var xmlHttp=null;try
     {
     // Firefox, Opera 8.0+, Safari
     xmlHttp=new XMLHttpRequest();
     }
    catch (e)
     {
     // Internet Explorer
     try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
     catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
     }
    return xmlHttp;
    }
    below is the .php page
    Code:
    <?php
    $q=$_GET["q"];
    
    $xmlDoc = new DOMDocument();
    $xmlDoc->load("cd_catalog.xml");
    
    $x=$xmlDoc->getElementsByTagName('ARTIST');
    
    for ($i=0; $i<=$x->length-1; $i++)
    {
    //Process only element nodes
    if ($x->item($i)->nodeType==1)
      {
      if ($x->item($i)->childNodes->item(0)->nodeValue == $q)
        { 
        $y=($x->item($i)->parentNode);
        }
      }
    }
    
    $cd=($y->childNodes);
    
    for ($i=0;$i<$cd->length;$i++)
    { 
    //Process only element nodes
    if ($cd->item($i)->nodeType==1)
      { 
      echo($cd->item($i)->nodeName);
      } 
    }
    
    ?>
    But it not works...please help...thanks.. thanks.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    When you say it doesn't work, can you be more specific? Are there any errors? If so, what is the message and on what line does it occur?

    Comment

    • perhapscwk
      New Member
      • Sep 2007
      • 123

      #3
      this part

      Code:
      function stateChanged() 
      { 
       if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
       { 
       document.getElementById("txtSingerName").value=xmlHttp.responseText 
       document.getElementById("txtAlbumName").value=xmlHttp.responseText 
       }
      and

      Code:
      for ($i=0;$i<$cd->length;$i++)
      { 
      //Process only element nodes
      if ($cd->item($i)->nodeType==1)
        { 
        echo($cd->item($i)->nodeName);
        } 
      }
      should be incorrect,,,

      how to use Ajax to display xml data and show it in different textbox?

      thanks.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You're setting the whole responseText to both text fields.

        Either parse the XML file in your PHP code to produce the information you need in a format that can be parsed with JavaScript, e.g. using a unique delimiter, or return a valid XML file, which can be parsed using the XML DOM - use getElementsByTa gName(tagName) to get the necessary information.

        Comment

        • perhapscwk
          New Member
          • Sep 2007
          • 123

          #5
          i using below but when i choose the opt box, it just chnage the textbox to blank.

          [code]

          <?php
          $q=$_GET["q"];

          $xmlDoc = new DOMDocument();
          $xmlDoc->load("cd_catal og.xml");

          $x=$xmlDoc->getElementsByT agName('ARTIST' );

          for ($i=0; $i<=$x->length-1; $i++)
          {
          //Process only element nodes
          if ($x->item($i)->nodeType==1)
          {
          if ($x->item($i)->childNodes->item(0)->nodeValue == $q)
          {
          $y=($x->item($i)->parentNode);
          }
          }
          }

          $cd=($y->childNodes);

          for ($i=0;$i<$cd->length;$i++)
          {
          //Process only element nodes
          if ($cd->item($i)->nodeType==1)
          {
          echo($cd->item($i)->nodeName);
          }
          }

          ?>

          the xml file i using the simple xml format.
          Code:
          <CATALOG>
          	<CD>
          		<TITLE>Empire Burlesque</TITLE>
          		<ARTIST>Bob Dylan</ARTIST>
          		<COUNTRY>USA</COUNTRY>
          		<COMPANY>Columbia</COMPANY>
          		<PRICE>10.90</PRICE>
          		<YEAR>1985</YEAR>
          	</CD>
          	<CD>
          		<TITLE>Hide your heart</TITLE>
          		<ARTIST>Bonnie Tyler</ARTIST>
          		<COUNTRY>UK</COUNTRY>
          		<COMPANY>CBS Records</COMPANY>
          		<PRICE>9.90</PRICE>
          		<YEAR>1988</YEAR>
          	</CD>
          </CATALOG>
          i want to use ajax and when i choose a artist from option box, it will return 2 values in 2 different textbox such as artist name and maybe company name.

          please help..

          thanks so much.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Your problem might be that "Bob" doesn't match "Bob Dylan".

            First check if your PHP works by giving it the string "?q=Bob", i.e. execute the PHP file "getcd.php?q=Bo b" and see what the output is.

            Comment

            Working...