Autofill form fields, almost working | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| |
Hey Everyone,
Well after asking many questions i have this almost working.
This is how it works. Basically i fill in my customer number field an that populates my drop down box. Once i select an option from the drop down box it populates the rest of the form. The problem i am having is that if a customer number doesn't exist in the drop down box an i try to put a new customer number in the input say 538 an then click on another field such as first name i get an error saying Object expected. An well i can't figure out why its saying that. An could really use some help as to why i am getting this error.
i started a topic earlier about this which you can see here. don't know if this will help in anyway but thought i would post it anyway. http://bytes.com/forum/showthread.ph...05#post3315805
here is my form - Customer Number*:<input type="text" name="custnum" id="clientID" value="" ONKEYUP="autoComplete(this,this.form.options,'value',false)" onChange="validate(this.form.custnum,'Customer Number')"/>
-
<SELECT NAME="options" id="options"
-
onChange="this.form.custnum.value=this.options[this.selectedIndex].value;javascript:initFormEvents();">
-
<cfoutput query="getcustnum">
-
<option value="#fk_custNum#">#fk_custNum#</option>
-
</cfoutput>
-
</SELECT>
-
First name: <input type="text" name="fname" id="fname" value="" size="20"/>
here is the javascript that appears on my form. - <SCRIPT LANGUAGE="JavaScript" SRC="autocomplete.js"></SCRIPT>
-
<script type="text/javascript" src="ajax.js"></script>
-
<script type="text/javascript">
-
-
var ajax = new sack();
-
var currentClientID=false;
-
function getClientData()
-
{
-
var clientId = document.getElementById('clientID').value;
-
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'];
-
eval(ajax.response);
-
}
-
-
-
function initFormEvents()
-
{
-
document.getElementById("options").onchange = getClientData;
-
}
-
-
window.onload = initFormEvents;
-
</script>
-
heres whats on my autocomplete.js file - 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; }
-
if (field.createTextRange) {
-
if (forcematch && !found) {
-
field.value=field.value.substring(0,field.value.length-1);
-
return;
-
}
-
var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
-
if (cursorKeys.indexOf(event.keyCode+";") == -1) {
-
var r1 = field.createTextRange();
-
var oldValue = r1.text;
-
var newValue = found ? select.options[i][property] : oldValue;
-
if (newValue != field.value) {
-
field.value = newValue;
-
var rNew = field.createTextRange();
-
rNew.moveStart('character', oldValue.length) ;
-
rNew.select();
-
}
-
}
-
}
-
}
here what appears on my ajax.js file - 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();
-
}
Thank you in advance,
Rach
| | Familiar Sight | | Join Date: Feb 2007
Posts: 207
| | | re: Autofill form fields, almost working Quote:
Originally Posted by bonneylake -
<SELECT NAME="options" id="options"
-
onChange="this.form.custnum.value=this.options[this.selectedIndex].value;javascript:initFormEvents();">
You can't name a <select> 'options' since it has such a property already and it's causing an addressing error.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Logician,
Well i tried that but i am still getting the same error. an i had to make some changes because i realized after i tried to do what you told me that my form was not even auto populating. An i also realized i am getting the same error when i try to create a new customer number an also when i autopopulate the form which makes me wonder if maybe the problem is with onchange? but here is the parts i changed everything else is the same.
javascript on form page - var ajax = new sack();
-
var currentClientID=false;
-
function getClientData()
-
{
-
var clientId = document.getElementById('clientID').value;
-
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'];
-
eval(ajax.response);
-
}
-
-
-
function initFormEvents()
-
{
-
document.getElementById('clientID').onblur = getClientData;
-
document.getElementById('clientID').focus();
-
document.getElementById("options").onchange = getClientData;
-
}
-
-
-
window.onload = initFormEvents;
html part - Customer Number*:<input type="text" name="custnum" id="clientID" value="" ONKEYUP="autoComplete(this,this.form.options,'value',false)" onChange="validate(this.form.custnum,'Customer Number')"/>
-
<SELECT NAME="customer" id="options"
-
onChange="this.form.custnum.value=this.options[this.selectedIndex].value;javascript:initFormEvents();">
-
<cfoutput query="getcustnum">
-
<option value="#fk_custNum#">#fk_custNum#</option>
-
</cfoutput>
-
</SELECT>
Thank you for all the help :),
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
The reason the auto-complete isn't working is that you forgot to change this line - autoComplete(this,this.form.options,'value',false)
to refer to the new name: - autoComplete(this,this.form.customer,'value',false)
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder,
the autocomplete is working. but i am still getting the error of object expected.
here is what i changed - Customer Number*:<input type="text" name="custnum" id="clientID" value="" ONKEYUP="autoComplete(this,this.form.customer,'value',false)" onChange="validate(this.form.custnum,'Customer Number')"/>
-
<SELECT NAME="customer" id="options"
-
onChange="this.form.custnum.value=this.options[this.selectedIndex].value;javascript:initFormEvents();">
-
<cfoutput query="getcustnum">
-
<option value="#fk_custNum#">#fk_custNum#</option>
-
</cfoutput>
-
</SELECT>
any suggestions, Thank you :),
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
What line does the error occur on? Check the error console in a decent browser such as Firefox - it'll give you more information about the error, if indeed it occurs in that browser.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder,
when i tried it out in firefox it says validate is not defined.also, when i try to type in a different customer number an go to another field it gets rid of what i typed in customer number in firefox, but it doesn't do this in internet explorer just gives me an error.
Thank you,
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
Well, that's a lot more useful. Have you actually defined validate() anywhere?
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder,
as far as i know, no. I looked through all the files i have for the word validate. An only place that has validate is here -
Customer Number*:<input type="text" name="custnum" id="clientID" value="" ONKEYUP="autoComplete(this,this.form.customer,'value',false)" onChange="validate(this.form.custnum,'Customer Number')"/>
i took validate out an i don't get the error anymore when i autopopulate or type something different into the customer number, so can i leave the validate part out of it? heres it updated without validate - Customer Number*:<input type="text" name="custnum" id="clientID" value="" ONKEYUP="autoComplete(this,this.form.customer,'value',false)" onChange="this.form.custnum,'Customer Number'"/>
However, i am still having trouble with it. when i type something into customer number an it doesn't exist in the drop down an i go to click into another field it completely gets ride of what i typed in the customer number field. Any suggestions on how to fix that?
Thank you :),
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
Get rid of the onchange completely. You must have copied the code from somewhere that used a validate() function onchange.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder,
The only thing wrong with taking out onchange is that then it doesn't populate the drop down box right. Like lets say i have 2 customer number values one called 4 and one called 444. If i just type 4 its putting 444 an when i go and take out the extra 2 4's its not changing the drop down box to 4 and instead its leaving it as 444 even though i typed in just 4(i hope that makes since) .
an the way i am trying to get it to work is if i start typing in customer number , as i am typeing in the customer number it populates the drop down box an then when you choose the value in the drop down box it populates the form.
but can i leave onchange in there or is that going to continue to cause trouble?
Thank you,
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
Does the following not work? - Customer Number*:<input type="text" name="custnum" id="clientID" value="" ONKEYUP="autoComplete(this,this.form.customer,'value',false )"/>
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Acoder,
it does, but then it doesn't work. ok heres an example. lets say i type 1. 1 is the name of the customer number i want. But its trying to guess the number so behind the 1 in my input field it adds 024 (which is another field called 1024), an would like to get it to stop guessing the number for me in my input field as well if thats possible. but anyway it puts 1024 also in my dropdown box. well when i remove the rest of the 1024 and put 1 it doesn't update the dropdown box from 1024 to 1 and instead remains 1024. however, it does populate the rest of the form with the correct information. Just need the drop down box to work correctly. Hope this makes since.
Thank you,
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
I tested with this simple example and it works fine: - <form>
-
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="1">1</option>
-
<option value="1024">1024</option>
-
</SELECT>
-
</form>
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder,
yep you are right your example works like it needs to. Typed yours in an it worked. But what i have is not working at all an makes no since to me! When i start typing in something it keeps auto filling the input field with a value thats in the dropdown box.like if i type 1 it puts 024 in blue right behind it in the input field and makes the drop down box 1024. It doesn't even seem to recognize 1 because when i type 1, 1 doesn't come up only the 1024 number. But when i click off of it, it auto fills the form with the values from customer number 1 it just doesn't change the drop down box from 1024 to 1.
but heres the form again - Customer Number*:<input type="text" name="custnum" id="clientID" value="" ONKEYUP="autoComplete(this,this.form.customer,'value',false)" <!---onChange="this.form.custnum,'Customer Number'"--->/>
-
<SELECT NAME="customer" id="options"
-
onChange="this.form.custnum.value=this.options[this.selectedIndex].value;<!---javascript:initFormEvents();--->">
-
<cfoutput query="getcustnum">
-
<option value="#fk_custNum#">#fk_custNum#</option>
-
</cfoutput>
-
</SELECT>
an here is the javascript on the form page. - <script type="text/javascript">
-
-
var ajax = new sack();
-
var currentClientID=false;
-
function getClientData()
-
{
-
var clientId = document.getElementById('clientID').value;
-
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'];
-
eval(ajax.response);
-
}
-
-
-
function initFormEvents()
-
{
-
document.getElementById('clientID').onblur = getClientData;
-
document.getElementById('clientID').focus();
-
document.getElementById("options").onchange = getClientData;
-
}
-
-
-
window.onload = initFormEvents;
-
</script>
-
Thank you for all the help,
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
What values does the getcustnum query return? In other words, what values do you have for the options for the select drop-down? View the source in your browser.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder,
According to it i have 1 and 1024 in there. i viewed the drop down and looked at the source. - <option value="222">222</option>
-
-
<option value="777">777</option>
-
-
<option value="555">555</option>
-
-
<option value="333">333</option>
-
-
<option value="32">32</option>
-
-
<option value="234">234</option>
-
-
<option value="324">324</option>
-
-
<option value="3">3</option>
-
-
<option value="1024">1024</option>
-
-
<option value="666">666</option>
-
-
<option value="444">444</option>
-
-
<option value="1">1</option>
-
-
<option value="1111">1111</option>
-
-
<option value="3333">3333</option>
i am baffled.
Thank you,
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
The most likely cause is that values need to be sorted, so modify your query to return the values in order.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder,
Alrighty i got that right so now 1 comes up before the other (yay). But still running into another problem *sigh*. When i try to type something into customer number that doesn't exist in the drop down an go to another field it complete deletes what i had tried to type before.
heres the html - 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;javascript:initFormEvents();">
-
<cfoutput query="getcustnum">
-
<option value="#fk_custNum#">#fk_custNum#</option>
-
</cfoutput>
-
</SELECT>
heres the javascript that appears with the html - var ajax = new sack();
-
var currentClientID=false;
-
function getClientData()
-
{
-
var clientId = document.getElementById('clientID').value;
-
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'];
-
eval(ajax.response);
-
}
-
-
-
function initFormEvents()
-
{
-
document.getElementById('clientID').onblur = getClientData;
-
document.getElementById('clientID').focus();
-
document.getElementById("options").onchange = getClientData;
-
}
-
-
-
window.onload = initFormEvents;
Thank you,
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
Why are you calling initFormEvents() in the onchange?
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder,
well i did that so that when i click on the drop down box it will auto fill the form fields instead of when i type in the value into the input box an then click enter or go to click into another field. Could that be causing the problem?
Thank you,
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
In that case, you just need to call getClientData() instead of initFormEvents.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder,
alrighty did that but i am still getting errors. When i did it on internet explorer i got unterminated string constant as the error. an when i did it in firefox i got this error
unterminated string literal an then this line - formObj.notes.value = "notes
heres the javascript that is on the form. - <SCRIPT LANGUAGE="JavaScript" SRC="autocomplete.js"></SCRIPT>
-
<script type="text/javascript" src="ajax.js"></script>
-
<script type="text/javascript">
-
-
var ajax = new sack();
-
var currentClientID=false;
-
function getClientData()
-
{
-
var clientId = document.getElementById('clientID').value;
-
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'];
-
eval(ajax.response);
-
}
-
-
-
function initFormEvents()
-
{
-
document.getElementById('clientID').onblur = getClientData;
-
document.getElementById('clientID').focus();
-
document.getElementById("options").onchange = getClientData;
-
}
-
-
-
window.onload = initFormEvents;
-
</script>
an here is the form itself - 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;javascript:getClientData();">
-
<cfoutput query="getcustnum">
-
<option value="#fk_custNum#">#fk_custNum#</option>
-
</cfoutput>
-
</SELECT>
Thank you,
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
That's a line in your Coldfusion code. Check that it's returning the correct data by running the Coldfusion page with the customer number that you're testing with. See what the output is.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder,
Yep i am getting the right information. i was messing with it earlier an somehow got the error to go away (not sure what i did). But now it is working correctly. I thought i had information in customer number 1024, but i guess i didn't which is why nothing was working right (blond moment).
but i got one more question. how would i make it so instead of if auto populating after i type the number in an try to do something else. how could i make it so that it will only autopopulate once i select the option that auto populated in the drop down box?
Thank you again for all the help :),
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
Remove this line: - document.getElementById('clientID').onblur = getClientData;
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder,
Alrighty i did that but i got one last problem. When i type in a number in the customer number. an the drop down box says the same number as the customer number. when i go to click on it, it wont autofill. if i choose another option it will autofill with that customer number information. an if i go back to the number that i was trying to autofill in the first place it will. but when i go to click on it when i first tell it to autopopulate it wont. any suggestions? - function initFormEvents()
-
{
-
<!---document.getElementById('clientID').onblur = getClientData;--->
-
document.getElementById('clientID').focus();
-
document.getElementById("options").onchange = getClientData
-
-
}
Thank you,
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
You could try adding this line to initFormEvents: - document.getElementById("options").onblur = getClientData;
The only problem with it is that it could result in a lot of unnecessary Ajax calls. To avoid that, you could check that a request has not already been made via onchange.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder,
is there anyway i could do something besides onblur?because when i do onblur it wont autofill the form until i click on another field. an well i don't got the smartest tools in the shed where i work an they would think it was not working.so was just wondering if i could use something else? trying to think of an easy way that it will do it an not confuse the people who use it. i was thinking of doing onclick. but if you do onclick then it will show all the dropdown fields an then it might confuse them on which number they actually choose. so if you got a suggestion on something i could use that would do the job an not confuse them that be great :).
here is what i got in the function intFormEvents - function initFormEvents()
-
{
-
<!--- document.getElementById('clientID').onblur = getClientData;--->
-
document.getElementById("options").onblur = getClientData;
-
document.getElementById('clientID').focus();
-
document.getElementById("options").onchange = getClientData;
-
-
-
}
but thank you for all the help :),
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
What you can do is use a button and call the function onclick.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder,
Well i like your idea so i added a button so now when they click on the button it inserts.
I am only running into 1 last problem. The thing with the customer number is that it appears on 2 pages. The first page is where you fill it out. An then the second page is where you view what you had typed in on the first page and also make changes in case of an error. The problem i am having is on the 2nd page.
If i do not select something from the drop down box an instead create a new customer number and click submit on the first page. Then on the second page it will bring up an error saying the form field can not be found (for the drop down box). An was wondering how could i give it the value of nothing if a value in the drop down box is not selected?
here is what i got in the form - Customer Number*:<input type="text" name="custnum" value="#Form.custnum#" id="clientID" ONKEYUP="autoComplete(this,this.form.customer,'value',false)" onChange="validate(this.form.custnum,'Customer Number')"/></cfoutput>
-
<SELECT NAME="customer" id="options"
-
onChange="this.form.custnum.value=this.options[this.selectedIndex].value;javascript:getClientData();">
-
<cfoutput query="getcustnum">
-
<option value="#fk_custNum#" <cfif #fk_custNum# is #form.customer#>selected</cfif>>#fk_custNum#</option>
-
</cfoutput>
-
</SELECT>
-
<input type="button" class="custnum" name="insert" value="Insert Contact"
-
onclick="javascript:getClientData();">
-
Thank you in advance for all the help :),
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
Do you have an empty value for the drop-down? You should have this selected initially. If you enter a new number that doesn't match, this is the value that should be selected.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder,
No i don't have a empty value for the drop-down. An when the drop down box loads when i fill out the information for the first time it says 1. but I changed it now so when it loads it says nothing.
this is what i did for the first page on the select button - <SELECT NAME="customer" id="options"
-
onChange="this.form.custnum.value=this.options[this.selectedIndex].value;">
-
<cfoutput query="getcustnum">
-
<option value="" selected></option>
-
<option value="#fk_custNum#">#fk_custNum#</option>
-
</cfoutput>
-
</SELECT>
but i am not sure how to apply this to the second page since its suppose to get whatever was choosen/writen on the first page.the only way i can think of is to do it in the table but if i do it in the table i have to put 0 an can't just leave it saying nothing in the table.
here is what i have on the second page right now but this doesn't worth with no value in there - <SELECT NAME="customer" id="options"
-
onChange="this.form.custnum.value=this.options[this.selectedIndex].value;javascript:getClientData();">
-
<cfoutput query="getcustnum">
-
<option value=""></option>
-
<option value="#fk_custNum#" <cfif #fk_custNum# is #form.customer#>selected</cfif>>#fk_custNum#</option>
-
</cfoutput>
-
</SELECT>
any suggestions? Thank you again for the help,
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
If the second page is only for viewing the results, just display the results using Coldfusion. The user can always go back to change something.
PS. you don't need the "javascript:" protocol in most cases, especially not in event handlers.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder
about the javsacript protocol are you meaning to take this part out javascript:getClientData()?
an well the thing is i am trying to display the one that is selected from the first page onto the second page so basically would it still work if i took the cfif selected part out?
Thank you,
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
Just the "javascript:" part, so for example, "javascript:getClientData()" becomes getClientData().
The second question is about Coldfusion, but I'll answer it here. Use a cfparam for the form.customer to set a default value of "".
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working Quote:
Originally Posted by acoder Just the "javascript:" part, so for example, "javascript:getClientData()" becomes getClientData().
The second question is about Coldfusion, but I'll answer it here. Use a cfparam for the form.customer to set a default value of "". Hey Acoder,
yep that worked,yay :). but i got one last problem. which sorry took so long to respond to this just got to come back to work today. I added one line to my drop down box. the line is bolded. - <SELECT NAME="customer" id="options"
-
onChange="this.form.custnum.value=this.options[this.selectedIndex].value;">
-
<cfoutput query="getcustnum">
- <option value="" selected></option>
-
<option value="#fk_custNum#">#fk_custNum#</option>
-
</cfoutput>
-
</SELECT>
but anyway with that line in there it makes the drop down options appear as such
1024
444
etc....
when needs to appear like this
1024
444
etc....
how could i still keep that line in there without it display my drop down box with spaces in it?
Thank you again for all the help :),
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
You need to keep that line outside the loop (before it), otherwise in the loop, you're adding two options for each iteration.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working Quote:
Originally Posted by acoder You need to keep that line outside the loop (before it), otherwise in the loop, you're adding two options for each iteration. Hey Acoder,
yep that was it. wow can't believe it's working! But THANK YOU SO MUCH FOR ALL THE HELP!!! you have no idea how awesome it is to see it working :). But thank you again for all the help, you are awesome!
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
You're welcome. Glad it's working. Pretty long thread for one which has got "almost working" in the title! ;)
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working Quote:
Originally Posted by acoder You're welcome. Glad it's working. Pretty long thread for one which has got "almost working" in the title! ;) Hey Acoder,
yeah i thought it was "almost working" but i was wrong. An still wrong now. i got another problem an i can't even begin to really explain it. This morning i got on to the form an when i tried to auto fill the form it wouldn't work. i checked the database an it has entry's. But when i tried to auto fill it it wouldn't worked. After refreshing it a few times an messing with it i got it to work. But today i have noticed this problem a lot. when i came back to lunch it didn't work. an when i let it sit for a few minutes an tried to change it i ran into this problem as well. an i am wondering could anything in the autofill and the drop down box affect this problem or could it be a loading problem on my part? i know we been having loading problems at work. but everything else for my form works fine even with the slow internet an few problems we have had. If you got any suggestions let me know because needs to work all the time an this seems to continue to be a problem. if you need me to repost everything i got i will.
Thank you,
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
Have you made any changes since the last version that you've posted in this thread?
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working Quote:
Originally Posted by acoder Have you made any changes since the last version that you've posted in this thread? Hey Acoder,
As far as i know i don't think so, the only thing i think i might of changed is the Javascript that appears with the form, i know i didn't mess with any of the files that work with it. Here is what i got on the Javascript part.i messed with the function initFormEvents part.
Javascript that appears on the form page - <script type="text/javascript">
-
-
var ajax = new sack();
-
var currentClientID=false;
-
function getClientData()
-
{
-
var clientId = document.getElementById('clientID').value;
-
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'];
-
eval(ajax.response);
-
}
-
-
-
function initFormEvents()
-
{
-
<!--- document.getElementById('clientID').onblur = getClientData;--->
-
document.getElementById("insert").onclick = getClientData;
-
<!--- document.getElementById('clientID').focus();
-
document.getElementById("options").onchange = getClientData;--->
-
-
-
}
-
-
-
window.onload = initFormEvents;
-
</script>
an here is the html i have in case you wanna see this. - 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>
-
<input type="button" class="custnum" name="insert" value="Insert Contact"
-
onclick="getClientData();">
Thank you :),
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
The initFormEvents function is now pretty much useless because you've already set the onclick inline in the button.
When you say it sometimes doesn't work, what exactly do you mean? Can you give an example? You do realise that with the current code, the form will only autofill on clicking the button?
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder,
well basically i would get on there an choose a number an then click on the button an it wouldn't auto fill the form. i ran in to this problem at certain points in the day yesterday.at one point it would work. an at others i would click on the button several times saying come on auto fill. i even had a point where i choose a number an clicked on the button and nothing happened, so i went online an checked my email i came back a minute later and the form was auto filled. but today it seems to be working fine. i might of been doing something wrong on account of i ended up getting a cold over the weekend so my mind is not as there so i might of chosen the wrong customer number. although thought i tried several different ones. it could of been a problem with the intranet because the networking guys had to fix some problems yesterday so it could be a mixture of both. but for now i am just going to keep an eye out on it an see what happens.
But thank you again for the help :),
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
OK, that's good to hear. You should also test the Coldfusion side of things when it doesn't seem to be working to see if it's giving the correct output.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working
Hey Acoder,
The error came up today. An i did the test like you said an its getting the correct information. But today it finally told me what is wrong an it says it has a problem with the following line. the line is bolded. The error it gives me is undetermined string constant - function showClientData()
-
{
-
var formObj = document.forms['page1'];
-
eval(ajax.response);
-
}
-
Any ideas? here is what firefox told me
Error: document.getElementById("insert") has no properties
Line: 66
Error: unterminated string literal
Line: 96, Column: 28
Source Code:
formObj.notes.value = "This is a test
here is the javascript that appears on the form page (which is what is having a problem) - <script type="text/javascript">
-
-
var ajax = new sack();
-
var currentClientID=false;
-
function getClientData()
-
{
-
var clientId = document.getElementById('clientID').value;
-
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'];
-
eval(ajax.response);
-
}
-
-
-
function initFormEvents()
-
{
-
-
document.getElementById("insert").onclick = getClientData;
-
-
-
-
}
-
-
-
window.onload = initFormEvents;
-
</script>
Thank you :),
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
This is caused by the Coldfusion-generated code where the string has not been terminated properly. Check the output. What does it show?
PS. I think you could probably take this back to the Coldfusion thread.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Autofill form fields, almost working Quote:
Originally Posted by acoder This is caused by the Coldfusion-generated code where the string has not been terminated properly. Check the output. What does it show?
PS. I think you could probably take this back to the Coldfusion thread. Hey Acoder,
i tried the getClient="" and it gave me the correct choice. The only thing i did to it was i took all the previously enter entrys out of the table an now wont work at all.
an also it is stll updating the drop down. if i add a new one an then go back you will see the new one. its just not auto populating the form.
Thank you,
Rach
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Autofill form fields, almost working
As I mentioned in the other thread, if you've got no data, it can't populate with anything.
|  | | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|