...

View Full Version : Search stopped working



MisterYoji
10-25-2011, 08:55 PM
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.


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




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

hdewantara
10-27-2011, 05:55 AM
Hi MisterYoji,
Instead of trapping submit event through a button, you might want to use onsubmit of the form itself. Something like below:

<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/forumdisplay.php?f=2.

Hendra



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum