473,586 Members | 2,695 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

123 New Member
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 1917
acoder
16,027 Recognized Expert Moderator MVP
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 New Member
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 Recognized Expert Moderator MVP
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.
May 14 '08 #4
perhapscwk
123 New Member
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.
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 Recognized Expert Moderator MVP
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.
May 14 '08 #6

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

Similar topics

2
2460
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. Regards, Manish Naik
2
1605
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 huge, so when I store it, it ask me if I want to pack it (the article) into a object then I click 'Yes'. But now I need to retrive data from the...
0
1270
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 value 8080 from the file and assign it to portno,so that when i am using portno in my program it will be replaced by 8080.
9
2848
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... here are my codings for writing a single row to a file...
23
3399
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 as fields. Also in policy table the firlds are:- ID, date_of_policy, no_policy, amount_paid, amount_balance and similarly the 3rd ie., staff has few...
1
5060
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 textbox(es) and the submit button showing until another option is selected. Also the data that's submitted is showing on the form page. Here's an...
2
1411
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 there a way to override the preexisting data in the txtbox when I click on it, and then enter a different number? Cheers Ed
2
3144
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 i change something in php file using in ajax function.it not refreshed,means its shows the previous result it not get updated.i can't understand...
5
4445
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 if I have the entirely wrong approach, but my code is below. Any or all help is appreciated! What currently happens is that the getdivision.php...
0
7908
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8199
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8336
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2343
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.