Hey Everyone,
Right now i have a script where when someone starts typing in the input field that it populates the drop down box next to it. Once both the input and drop down box have the same number then it autofills the rest of the form. This works great. But now i have realized that lets say i have a previous entry of a00092 but i am creating a new entry an it is a0 Instead of the drop down box going blank since the input don't match the drop down box, the drop down box remains a00092. If i keep typing past a0 with more values like a02 it will make the drop down box blank, but if i stop at a0 it will leave the drop down box with a value.
I was wondering if there was a way to make sure that if i have a0 in the the field an i go an click off of it or click on another field or someway to make it where that once i continue to fill out the form that the drop down box will not continue to say a00092 an will instead go blank.
Here is the html
javascript that appears on html page.
autocomplete.js this autocomplets the form
autofill.js this autofills the form
Thank you in advance,
Rach
Right now i have a script where when someone starts typing in the input field that it populates the drop down box next to it. Once both the input and drop down box have the same number then it autofills the rest of the form. This works great. But now i have realized that lets say i have a previous entry of a00092 but i am creating a new entry an it is a0 Instead of the drop down box going blank since the input don't match the drop down box, the drop down box remains a00092. If i keep typing past a0 with more values like a02 it will make the drop down box blank, but if i stop at a0 it will leave the drop down box with a value.
I was wondering if there was a way to make sure that if i have a0 in the the field an i go an click off of it or click on another field or someway to make it where that once i continue to fill out the form that the drop down box will not continue to say a00092 an will instead go blank.
Here is the html
Code:
Customer Number*:<input type="text" name="custnum" id="clientID" value="" ONkeyup="autoComplete(this,this.form.customer,'value',false)"/> <SELECT NAME="customer" id="options" onChange="this.form.custnum.value=this.options[this.selectedIndex].value;"> <option value="" selected></option> <cfoutput query="getcustnum"> <option value="#fk_custNum#">#fk_custNum#</option> </cfoutput> </SELECT>
javascript that appears on html page.
Code:
<SCRIPT LANGUAGE="JavaScript" SRC="autocomplete.js"></SCRIPT>
<script type="text/javascript" src="autofill.js"></script>
<script type="text/javascript">
var ajax = new sack();
var currentClientID=false;
function getClientData()
{
var clientId = document.getElementById('clientID').value;
var customer = document.getElementById('options').value;
if (clientId != "" && customer != "") {
currentClientID = clientId
ajax.requestFile = 'getClient.cfm?custnum='+clientId; // Specifying which file to get
ajax.onCompletion = showClientData; // Specify function that will be executed after file has been found
ajax.runAJAX(); // Execute AJAX function
}}
function showClientData()
{
var formObj = document.forms['page1'];
var resp = ajax.response.split(",");
if (ajax.response == "") { // you may want to trim here just in case
formObj.cust_company.value = "";
formObj.fname.value = "";
formObj.lname.value = "";
formObj.add1.value = "";
formObj.city.value = "";
formObj.state.value = "";
formObj.zip.value = "";
formObj.email.value = "";
formObj.pri_phone.value = "";
formObj.sec_phone.value = "";
formObj.notes.value = "";
} else {
var resp = ajax.response.split(",");
formObj.cust_company.value = resp[1];
formObj.fname.value = resp[2];
formObj.lname.value = resp[3];
formObj.add1.value = resp[4];
formObj.city.value = resp[5];
formObj.state.value = resp[6];
formObj.zip.value = resp[7];
formObj.email.value = resp[8];
formObj.pri_phone.value = resp[9];
formObj.sec_phone.value = resp[10];
formObj.notes.value = resp[11];
}
}
</script>
Code:
function autoComplete (field, select, property, forcematch) {
var found = false;
for (var i = 0; i < select.options.length; i++) {
if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
found=true; break;
}
}
if (found) { select.selectedIndex = i; }
else { select.selectedIndex = -1; }}
autofill.js this autofills the form
Code:
function sack(file) {
this.xmlhttp = null;
this.resetData = function() {
this.method = "POST";
this.queryStringSeparator = "?";
this.argumentSeparator = "&";
this.URLString = "";
this.encodeURIString = true;
this.execute = false;
this.element = null;
this.elementObj = null;
this.requestFile = file;
this.vars = new Object();
this.responseStatus = new Array(2);
};
this.resetFunctions = function() {
this.onLoading = function() { };
this.onLoaded = function() { };
this.onInteractive = function() { };
this.onCompletion = function() { };
this.onError = function() { };
this.onFail = function() { };
};
this.reset = function() {
this.resetFunctions();
this.resetData();
};
this.createAJAX = function() {
try {
this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e1) {
try {
this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
this.xmlhttp = null;
}
}
if (! this.xmlhttp) {
if (typeof XMLHttpRequest != "undefined") {
this.xmlhttp = new XMLHttpRequest();
} else {
this.failed = true;
}
}
};
this.setVar = function(name, value){
this.vars[name] = Array(value, false);
};
this.encVar = function(name, value, returnvars) {
if (true == returnvars) {
return Array(encodeURIComponent(name), encodeURIComponent(value));
} else {
this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
}
}
this.processURLString = function(string, encode) {
encoded = encodeURIComponent(this.argumentSeparator);
regexp = new RegExp(this.argumentSeparator + "|" + encoded);
varArray = string.split(regexp);
for (i = 0; i < varArray.length; i++){
urlVars = varArray[i].split("=");
if (true == encode){
this.encVar(urlVars[0], urlVars[1]);
} else {
this.setVar(urlVars[0], urlVars[1]);
}
}
}
this.createURLString = function(urlstring) {
if (this.encodeURIString && this.URLString.length) {
this.processURLString(this.URLString, true);
}
if (urlstring) {
if (this.URLString.length) {
this.URLString += this.argumentSeparator + urlstring;
} else {
this.URLString = urlstring;
}
}
// prevents caching of URLString
this.setVar("rndval", new Date().getTime());
urlstringtemp = new Array();
for (key in this.vars) {
if (false == this.vars[key][1] && true == this.encodeURIString) {
encoded = this.encVar(key, this.vars[key][0], true);
delete this.vars[key];
this.vars[encoded[0]] = Array(encoded[1], true);
key = encoded[0];
}
urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
}
if (urlstring){
this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
} else {
this.URLString += urlstringtemp.join(this.argumentSeparator);
}
}
this.runResponse = function() {
eval(this.response);
}
this.runAJAX = function(urlstring) {
if (this.failed) {
this.onFail();
} else {
this.createURLString(urlstring);
if (this.element) {
this.elementObj = document.getElementById(this.element);
}
if (this.xmlhttp) {
var self = this;
if (this.method == "GET") {
totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
this.xmlhttp.open(this.method, totalurlstring, true);
} else {
this.xmlhttp.open(this.method, this.requestFile, true);
try {
this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
} catch (e) { }
}
this.xmlhttp.onreadystatechange = function() {
switch (self.xmlhttp.readyState) {
case 1:
self.onLoading();
break;
case 2:
self.onLoaded();
break;
case 3:
self.onInteractive();
break;
case 4:
self.response = self.xmlhttp.responseText;
self.responseXML = self.xmlhttp.responseXML;
self.responseStatus[0] = self.xmlhttp.status;
self.responseStatus[1] = self.xmlhttp.statusText;
if (self.execute) {
self.runResponse();
}
if (self.elementObj) {
elemNodeName = self.elementObj.nodeName;
elemNodeName.toLowerCase();
if (elemNodeName == "input"
|| elemNodeName == "select"
|| elemNodeName == "option"
|| elemNodeName == "textarea") {
self.elementObj.value = self.response;
} else {
self.elementObj.innerHTML = self.response;
}
}
if (self.responseStatus[0] == "200") {
self.onCompletion();
} else {
self.onError();
}
self.URLString = "";
break;
}
};
this.xmlhttp.send(this.URLString);
}
}
};
this.reset();
this.createAJAX();
}
Rach
Comment