// —————————- script.css —————————————-
#popups {
position: absolute;
visibility:hidden;
}
#searchField.error {
background-color: #FC0;
}
// —————————- script.js —————————————-
window.onload = initAll;
var xhr = false;
var statesArray = new Array();
var passArray = new Array();
function initAll() {
document.getElementById(”searchField”).onkeyup = searchSuggest;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else {
if (window.ActiveXObject) {
try {
xhr = new ActiveXObject(”Microsoft.XMLHTTP”);
}
catch (e) { }
}
}
if (xhr) {
xhr.onreadystatechange = setStatesArray;
xhr.open(”GET”, “us-states.xml”, true);
xhr.send(null);
}
else {
alert(”Sorry, but I couldn’t create an XMLHttpRequest”);
}
}
function setStatesArray() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
if (xhr.responseXML) {
var allStates = xhr.responseXML.getElementsByTagName(”item”);
for (var i=0; i
statesArray[i] = allStates[i].getElementsByTagName(”label”)[0].firstChild;
}
}
}
else {
alert(”There was a problem with the request ” + xhr.status);
}
}
}
function searchSuggest() {
var str = document.getElementById(”searchField”).value;
document.getElementById(”searchField”).className = “”;
if (str != “”) {
document.getElementById(”popups”).innerHTML = “”;
var flag = 0;
for (var i=0; i
var thisState = statesArray[i].nodeValue;
if (str == thisState) {
flag = 1;
}
if (thisState.toLowerCase().indexOf(str.toLowerCase()) == 0) {
var tempDiv = document.createElement(”div”);
tempDiv.innerHTML = thisState;
//tempDiv.onclick = makeChoice;
tempDiv.className = “suggestions”;
document.getElementById(”popups”).appendChild(tempDiv);
}
}
var foundCt = document.getElementById(”popups”).childNodes.length;
if (foundCt == 0) {
document.getElementById(”searchField”).className = “error”;
document.getElementById(”submitme”).style.visibility=”hidden”;
}
if (foundCt > 0) {
if (flag == 1) {
document.getElementById(”submitme”).style.visibility=”visible”;
}
}
}
}
Javascript: Using window.XMLHttpRequest object, the AJAX connectivity starts. The client reads from an XML file, parse required data from it and use that information in the client end.
Then the value is compared against the given string. If it matches with the stored passwords, the ‘submit button’ appears (case B in the figure). However, any mismatch of both strings will keep the submit button hidden from viewing. Also the mismatch is shown by the yellow colour (case C in the figure). Note that case C indicates that the user-given string cannot be prefix of any stored passwords. Therefore user should delete and enter again.
// —————————- states.xml (passwords) —————————————-
………………………………….
…………………………………..
Conclusion: The advantage of this code is that you do not need to use any sort of standard database (access, sql database, etc). You simple put these four files in your server, change or populate the xml script with your passwords (of clients) and run the html. Now call from the server (e.g. using http://localhost/path..). Its working, isn’t it? You see all the complexity regarding standard database connectivity in the server sided script is not required. In many occasions, this simple script will remove your burden to setup and external database and sql coding within your script.
Happy simpler coding!
Manzur Ashraf
www.sacars.com.au
Cheap domain registration
Tags: Compatibility Issues, Flat Database, Query Button