By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,465 Members | 1,163 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,465 IT Pros & Developers. It's quick & easy.

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

100+
P: 123
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. <script src="selectcd.js"></script>
  3.  
  4. <form> 
  5. Select a CD:
  6. <select name="cds" onchange="showCD(this.value)">
  7. <option value="Bob">Bob</option>
  8. <option value="Bonnie">Bonnie</option>
  9. </select>
  10. </form><p>
  11.  
  12. <input type="text" name="txtSingerName" id="txtSingerName" size=20 class="stdbox" value="<?php print stripslashes($form_txtSingerName); ?>">
  13. <input type="text" name="txtAlbumName" id="txtAlbumName" size=20 class="stdbox" value="<?php print stripslashes($form_txtAlbumName); ?>">
  14. </p>
  15.  
below is the .js file
Expand|Select|Wrap|Line Numbers
  1. var xmlHttp
  2.  
  3. function showCD(str)
  4. xmlHttp=GetXmlHttpObject()
  5. if (xmlHttp==null)
  6.  {
  7.  alert ("Browser does not support HTTP Request")
  8.  return
  9.  } 
  10. var url="getcd.php"
  11. url=url+"?q="+str
  12. url=url+"&sid="+Math.random()
  13. xmlHttp.onreadystatechange=stateChanged 
  14. xmlHttp.open("GET",url,true)
  15. xmlHttp.send(null)
  16. }
  17.  
  18. function stateChanged() 
  19.  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  20.  { 
  21.  document.getElementById("txtSingerName").value=xmlHttp.responseText 
  22.  document.getElementById("txtAlbumName").value=xmlHttp.responseText 
  23.  } 
  24. }function GetXmlHttpObject()
  25. {
  26. var xmlHttp=null;try
  27.  {
  28.  // Firefox, Opera 8.0+, Safari
  29.  xmlHttp=new XMLHttpRequest();
  30.  }
  31. catch (e)
  32.  {
  33.  // Internet Explorer
  34.  try
  35.   {
  36.   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  37.   }
  38.  catch (e)
  39.   {
  40.   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  41.   }
  42.  }
  43. return xmlHttp;
  44. }
  45.  
below is the .php page
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $q=$_GET["q"];
  3.  
  4. $xmlDoc = new DOMDocument();
  5. $xmlDoc->load("cd_catalog.xml");
  6.  
  7. $x=$xmlDoc->getElementsByTagName('ARTIST');
  8.  
  9. for ($i=0; $i<=$x->length-1; $i++)
  10. {
  11. //Process only element nodes
  12. if ($x->item($i)->nodeType==1)
  13.   {
  14.   if ($x->item($i)->childNodes->item(0)->nodeValue == $q)
  15.     { 
  16.     $y=($x->item($i)->parentNode);
  17.     }
  18.   }
  19. }
  20.  
  21. $cd=($y->childNodes);
  22.  
  23. for ($i=0;$i<$cd->length;$i++)
  24. //Process only element nodes
  25. if ($cd->item($i)->nodeType==1)
  26.   { 
  27.   echo($cd->item($i)->nodeName);
  28.   } 
  29. }
  30.  
  31. ?>
  32.  
But it not works...please help...thanks..thanks.
May 13 '08 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
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?
May 14 '08 #2

100+
P: 123
this part

Expand|Select|Wrap|Line Numbers
  1. function stateChanged() 
  2.  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  3.  { 
  4.  document.getElementById("txtSingerName").value=xmlHttp.responseText 
  5.  document.getElementById("txtAlbumName").value=xmlHttp.responseText 
  6.  } 
  7.  
and

Expand|Select|Wrap|Line Numbers
  1. for ($i=0;$i<$cd->length;$i++)
  2. //Process only element nodes
  3. if ($cd->item($i)->nodeType==1)
  4.   { 
  5.   echo($cd->item($i)->nodeName);
  6.   } 
  7. }
  8.  
should be incorrect,,,

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

thanks.
May 14 '08 #3

acoder
Expert Mod 15k+
P: 16,027
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 getElementsByTagName(tagName) to get the necessary information.
May 14 '08 #4

100+
P: 123
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_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);
}
}

?>

the xml file i using the simple xml format.
Expand|Select|Wrap|Line Numbers
  1. <CATALOG>
  2.     <CD>
  3.         <TITLE>Empire Burlesque</TITLE>
  4.         <ARTIST>Bob Dylan</ARTIST>
  5.         <COUNTRY>USA</COUNTRY>
  6.         <COMPANY>Columbia</COMPANY>
  7.         <PRICE>10.90</PRICE>
  8.         <YEAR>1985</YEAR>
  9.     </CD>
  10.     <CD>
  11.         <TITLE>Hide your heart</TITLE>
  12.         <ARTIST>Bonnie Tyler</ARTIST>
  13.         <COUNTRY>UK</COUNTRY>
  14.         <COMPANY>CBS Records</COMPANY>
  15.         <PRICE>9.90</PRICE>
  16.         <YEAR>1988</YEAR>
  17.     </CD>
  18. </CATALOG>
  19.  
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.
May 14 '08 #5

acoder
Expert Mod 15k+
P: 16,027
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=Bob" and see what the output is.
May 14 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.