www.webdeveloper.com
Results 1 to 2 of 2

Thread: <<help selecting search xml source with dropdown>>

Hybrid View

  1. #1
    Join Date
    May 2009
    Posts
    4

    Smile <<help selecting search xml source with dropdown>>

    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...




    <!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:&nbsp;&nbsp;</b>
    Name<input id="searchme" type="text" size="20">&nbsp;&nbsp;
    <input value="Submit" onclick="searchIndex(); return false;" type="submit">
    </form>

    </body>
    </html>







    index.xml


    <?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


    <?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>

  2. #2
    Join Date
    Aug 2009
    Location
    Chile
    Posts
    34
    Looks to me like you are asking for the value of a dropdown but you need to specify that you want the value of the selectedIndex of the dropdown.

    You have:
    var docdoc = document.getElementById('urlfile').value;

    You need:
    document.getElementById('urlfile')[document.getElementById('urlfile').selectedIndex].value;

    This is all on one liine. It will put the value of the selected option in docdoc.

    That should do it as long as everything else is OK.

    Good Luck,

    Ken

    www.webdesigntips-foreveryone.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles