473,386 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

123 100+
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
5 1903
acoder
16,027 Expert Mod 8TB
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
perhapscwk
123 100+
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
16,027 Expert Mod 8TB
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
perhapscwk
123 100+
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
16,027 Expert Mod 8TB
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

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

Similar topics

2
by: Manish Naik | last post by:
Hi, Using ASP.Net, I want to store and retrive documents (Word, excel, etc) from SQL Server 2000 database. I have tried image type data field, but could not succed. Can any one help me please. ...
2
by: Terry | last post by:
Any .dll or COM+ for .Net can help me retrive data from a Access file? The table contain several columns and the last one is a container which I store Article; because each article is a little bit...
0
by: avishekb | last post by:
can anyone suggest me how to retrive data from a xml file using c/c++. for example: <schema> <applt to>portno</applyto> <type>int</type> <value>8080</value> </schema> i want to retrive the...
9
by: asenthil | last post by:
Hai to all,, i had to tried to retrive and write a single row to a file from the database.. But dont know to write all the retrived rows to a file from the database.. how to do that... ...
23
nehashri
by: nehashri | last post by:
hi i am designing a database using Ms Access and ASP. i have 3 tables in access namely 'PERSONAL', other as 'POLICY' and 3rd one is named as 'STAFF'. in the contact table i have ID, Name, Children...
1
by: sparksol | last post by:
I have a form with a drop down box. If you select an option in the drop down box (depending which option is selected) one or two textbox(es) and a submit button display. I would like to keep the...
2
by: ebasshead | last post by:
Hi Everybody, I have a txtbox that populates itself when the start of the form is filled out. But sometimes I have to change the info in that specific txtbox. At the moment it wont allow me to. Is...
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
5
by: thatcollegeguy | last post by:
Below are my 3php and 2js files. I create a table using ajax/php and then want to change the values in the tables add(+ number for teamid) id's for each specific td in the table. I don't know...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.