LarrySteele
Programmer
I used the ajax tutorial from w3schools and have successfully implemented a couple of ajax lookups. While somewhat straightforward, the process is not without quirks. I'm using the following code to lookup a person based on the id entered in a textbox:
Note the commented alert. If I uncomment the alert statement and let it run, the manager's name gets filled in the text box as desired. However, if I leave the alert commented so that it does not run, then I get an Object Expected error. In short, what appears to be happening is that this javascript needs to pause long enough for seekMgr to run. Putting in an alert is one way to do it. I'm sure there's a better way to do this, but I've hit a wall and haven't been able to locate a solution - yet.
Here is my javascript:
Here's ajax/getmanager.cfm that fetches the manager's information:
Again, this works flawlessly, but only as long as I have that alert between seekMgr() and actually using the input element that my ajax creates/fills.
Any assistance/suggestions are most appreciated.
- Larry
Code:
function find_manager() {
var var_mgr_id = document.calform.var_superv_id.value;
seekMgr(var_mgr_id);
[!]// alert('pause goes here');[/!]
var mgr_first_name = document.getElementById('inp_mgr_first_name').value;
alert('Manger\'s first name is: ' + mgr_first_name);
}
Note the commented alert. If I uncomment the alert statement and let it run, the manager's name gets filled in the text box as desired. However, if I leave the alert commented so that it does not run, then I get an Object Expected error. In short, what appears to be happening is that this javascript needs to pause long enough for seekMgr to run. Putting in an alert is one way to do it. I'm sure there's a better way to do this, but I've hit a wall and haven't been able to locate a solution - yet.
Here is my javascript:
Code:
var xmlHttp
function seekMgr(str) {
if (str.length != 8) {
document.getElementById("txt_mgr_results").innerHTML=''
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null) {
alert ("Browser does not support HTTP Request")
return
}
var url="ajax/getmanager.cfm"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged_mgr
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged_mgr() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
//alert('state change')
document.getElementById("txt_mgr_results").innerHTML=xmlHttp.responseText
//alert(xmlHttp.responseText)
}
}
function GetXmlHttpObject() {
var objXMLHttp=null
if (window.XMLHttpRequest) {
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject) {
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}
Here's ajax/getmanager.cfm that fetches the manager's information:
Code:
<cfquery name="qry_mgr"
datasource="#mydata#"
username="#myuser#"
password="#mypw#">
select last_name,
first_name
from mytable
where id= '#url.q#'
</cfquery>
<cfif qry_mgr.recordcount eq 0>
<cfoutput>
<input type="hidden" name="inp_mgr_first_name" value="">
<input type="hidden" name="inp_mgr_last_name" value="">
</cfoutput>
<cfelse>
<cfoutput query="qry_mgr">
<input type="hidden" name="inp_mgr_first_name" value="#first_name#">
<input type="hidden" name="inp_mgr_last_name" value="#last_name#">
</cfoutput>
</cfif>
Again, this works flawlessly, but only as long as I have that alert between seekMgr() and actually using the input element that my ajax creates/fills.
Any assistance/suggestions are most appreciated.
- Larry