Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Search stopped working

    I have a small search script that goes through an xml file and searches for whatever the user is looking for. I have no problem with it in firefox however in IE 9, hitting enter to search simply reloads the page and it also doesnt load any results. Heres my code, help would be greatly appreciated. Thanks in advance.

    Code:
    <script type="text/javascript">
    function getType() {
    	for (var i=0; i < 3; i++) {
    		if (document.frmMain.criteria[i].checked) {
    			var rad_val = document.frmMain.criteria[i].value;
    		}
    	}
    	return rad_val;
    }
    
    window.onload = loadIndex;
    
    function loadIndex() { // load indexfile
    // most current browsers support document.implementation
    	if (document.implementation && document.implementation.createDocument) {
    		xmlDoc = document.implementation.createDocument("", "", null);
    		xmlDoc.load("wdparts.xml");
    	}
    // MSIE uses ActiveX
    	else if (window.ActiveXObject) {
    		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    		xmlDoc.async = "false";
    		xmlDoc.load("wdparts.xml");
    	}
    }
    
    
    
    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 searchtype = getType();
    	var allitems = xmlDoc.getElementsByTagName("item");
    	results = new Array;
    	if (searchterm.length < 3) {
    		alert("Enter at least three characters");
    	} 
    	else {
    // see if the XML entry matches the search term,
    // and (if so) store it in an array		\
    		for (var i=0;i<allitems.length;i++) {
    			var name = allitems[i].getAttribute(searchtype);
    			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 
    		
    		var reout = 'You searched for <b><i>'+searchterm+'</i></b><br><br>';
    		reout += '<table border="1" style="width: 100%;">';
    		reout += '<tr><th>Manufacturer</th><th>Product Number</th><th>Description</th><th>Link</th></tr>';
    		for(var i=0; i<results.length; i++) {
    		 reout += '<tr>';
    		 reout += '<td>' + results[i].getAttribute("line") + '</td>';
    		 reout += '<td>' + results[i].getAttribute("pnum") + '</td>';
    		 reout += '<td>' + results[i].getAttribute("description") + '</td>';
    		 reout += '<td>' + results[i].getAttribute("link") + '</td>';
    		 reout += '</tr>';
    		}
    		reout += '<table>';
    		document.getElementById('results').innerHTML = reout;
    	} else {
    // else tell the user no matches were found
    		alert('No results found for '+searchterm+'!');
    	}
    }
    </script>


    Code:
    <div id="content"> <h1><p>Product Search: **UNDER CONSTRUCTION**</p></h1>
                
    <form name="frmMain" id="frmMain" action="">
    <fieldset>
    	Search by:
    	<input type="radio" name="criteria" value="line" checked="checked">Manufacturer &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;
    	<input type="radio" name="criteria" value="pnum">Product Number &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    	<input type="radio" name="criteria" value="description">Description &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    	<br /><br /> 
    
    	<input id="searchme" type="text" size="20" />&nbsp;&nbsp; 
    
    	<input type="submit" value="Submit" id="btnSearch" onClick="searchIndex(); return false;" />
    </fieldset>
    </form>
                    <p id="results"></p>
    
    
    		  </div>

  • #2
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    354
    Thanks
    9
    Thanked 52 Times in 51 Posts
    Hi MisterYoji,
    Instead of trapping submit event through a button, you might want to use onsubmit of the form itself. Something like below:
    Code:
    <form name="frmMain" id="frmMain" action="" onsubmit="searchIndex(); return false;">
    <fieldset>
    	Search by:
    	<input type="radio" name="criteria" value="line" checked="checked">Manufacturer &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;
    	<input type="radio" name="criteria" value="pnum">Product Number &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    	<input type="radio" name="criteria" value="description">Description &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    	<br /><br /> 
    
    	<input id="searchme" type="text" size="20" />&nbsp;&nbsp; 
    	<input type="submit" value="Submit" id="btnSearch" />
    	<!--<input type="submit" value="Submit" id="btnSearch" onClick="searchIndex(); return false;" />-->
    </fieldset>
    </form>
    But you probably get more responses in: http://www.codingforums.com/javascript-programming/.

    Hendra


  •  

    Posting Permissions

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