Hello guys! here is a search code that was working fine until now.. =( what im trying to do is to select the xml source to be searched using a dropdown.. but now i dont know what's wrong.. just take a look, and let me know if you get it right.. thxx! =)
note: the problem must be on loading the xml and/or dropdown select,, not on the search code...
HTML Code:<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"> <title>Search XML</title> <script type="text/javascript"> window.onload = loadIndex; function loadIndex() { // load indexfile // most current browsers support document.implementation if (document.implementation && document.implementation.createDocument) { var docdoc = document.getElementById('urlfile').value; xmlDoc = document.implementation.createDocument("", "", null); xmlDoc.load(docdoc); } // MSIE uses ActiveX else if (window.ActiveXObject) { var docdoc = document.getElementById('urlfile').value; var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async="false"; xmlDoc.load(docdoc); } } function searchIndex() { // search the index (duh!) if (!xmlDoc) { loadIndex(); } // get the search term from a form field with id 'searchme' var searchterm = document.getElementById("searchme").value; var allitems = xmlDoc.getElementsByTagName("item"); results = new Array; if (searchterm.length < 2) { alert("Please enter at least 2 digits"); } else { for (var i=0;i<allitems.length;i++) { // see if the XML entry matches the search term, // and (if so) store it in an array var name = allitems[i].lastChild.nodeValue; var exp = new RegExp(searchterm,"i"); if ( name.match(exp)!= null) { results.push(allitems[i]); } } // send the results to another function that displays them to the user showResults(results, searchterm); } } // Write search results to a table function showResults(results, searchterm) { if (results.length > 0) { // if there are any results, write them to a table document.write('<div><a href="searchxml.htm">New Search</a></div>You searched for <b><i>'+searchterm+'</i></b><br><br>'); document.write('<table border-top="3px" style="width: 100%; background-color: #cccccc; font-color:#ffffff; border-color:black;">'); document.write('<tr><th>NAME</th><th>AGE</th><th>HEIGHT</th><th>WEIGHT</th></tr>'); for(var i=0; i<results.length; i++) { document.write('<tr >'); document.write('<td>' + results[i].getAttribute("name") + '<br />'); document.write('<img src="' + results[i].getAttribute("image") + '"></td>'); document.write('<td >' + results[i].getAttribute("age") + '</td>'); document.write('<td>' + results[i].getAttribute("height") + '</td>'); document.write('<td>' + results[i].getAttribute("weight") + '</td>'); document.write('</tr>'); } document.write('<table>'); document.close(); } else { // else tell the user no matches were found var notfound = alert('No results found for '+searchterm+'!'); } } } </script> </head> <body > <select id="urlfile" onchange="loadIndex()"> <option value="index.xml"> XML 1</option> <option value="index2.xml"> XML 2</option> </select> <form action=""><b>Search: </b> Name<input id="searchme" type="text" size="20"> <input value="Submit" onclick="searchIndex(); return false;" type="submit"> </form> </body> </html>
index.xml
HTML Code:<?xml version="1.0" encoding="utf-8"?> <searchable_index> <item name="John" image="profile.png" age="22" height="5 ft 5 inches" weight="150">John</item> <item name="Paul" image="profile2.png" age="25" height="5 ft 9 inches" weight="168">Paul</item> <item name="George" image="profile3.png" age="27" height="6 ft 1 inches" weight="175">george</item> <item name="Ringo" image="profile4.png" age="23" height="6 ft 3 inches" weight="180">ringo</item> </searchable_index>
index2.xml
HTML Code:<?xml version="1.0" encoding="utf-8"?> <searchable_index> <item name="James" image="profile.png" age="22" height="5 ft 5 inches" weight="150">James</item> <item name="Patrick" image="profile2.png" age="25" height="5 ft 9 inches" weight="168">Patrick</item> <item name="Carl" image="profile3.png" age="27" height="6 ft 1 inches" weight="175">Carl</item> <item name="Ronny" image="profile4.png" age="23" height="6 ft 3 inches" weight="180">Ronny</item> </searchable_index>



Reply With Quote
Bookmarks