I was reading up on some XML and AJAX stuff the other day and came across the XMLHttpRequest Object - when I come across snippets of code like the example in the link, I always try to figure a way to incorporate it into my work. I always ask myself is this something I can use and make my software more efficient?
This was something I most definitely could use as we have a system of part numbers in our database. We have users that select part numbers to enter Selling Opportunities so I was able to use the code to look up the part numbers more quickly, showing the part numbers, qty on hand, and some other pertinent info.
Below is an example of some code:
Page1.asp
<html>
<head>
<script>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "test_source2.asp?q=" + str, true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<p><b>Start typing a Part Number in the input field below:</b></p>
<form>
Part Num: <input type="text" onkeyup="showHint(this.value)">
</form>
<p><span id="txtHint"></span></p>
</body>
</html>
Here's the test_source2.asp code:
response.expires=-1
q=ucase(request.querystring("q"))
sql = "SELECT top 50 partnumfield, qty FROM partTable with (nolock) where partnumfield like '" & q & "%' ORDER By partnumfield"
set rs = conn.execute(sql)
if len(q) > 0 then
if not rs.bof and not rs.eof then
do until rs.eof
hint = hint & rs("partnumfield") & " (" & rs("qty") & ")<br>"
rs.movenext
loop
end if
rs.close
set rs = nothing
end if
if hint="" then
response.write("no matches")
else
response.write(hint)
end if
Comments
Post a Comment