Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with adding parameter to a function 1

Status
Not open for further replies.

dr00bie

Programmer
Feb 19, 2004
108
US
I am very new to AJAX, and have been fiddling around with it to make some new interfaces. I have the following js code that I swiped from w3schools.com,

Code:
var xmlHttp

function showName(RegNo)
{
if (RegNo.length==0)
{ 
document.getElementById("txtHint").innerHTML=""
return
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}        
var url="db/cli/shared/getresidentname.asp"
url=url+"?RegNo="+RegNo
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
} 

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
   { 
   document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
   } 
} 

function GetXmlHttpObject()
{ 
var objXMLHttp=null
if (window.XMLHttpRequest)
  {
  objXMLHttp=new XMLHttpRequest()
  }
else if (window.ActiveXObject)
  {
  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
return objXMLHttp
}

The body of my page looks like this,

Code:
<body>
<form name="form1" id="form1"> 
First Name:
<input type="text" id="txt1" onBlur="showName(this.value)">
</form>
<p>First Name: <span id="txtName"></span></p>
</body>

I am trying to add another parameter to the function, so I can call the function using a different span element name. I have tried this,

Code:
var xmlHttp

function showName(RegNo,ctlName)
{
if (RegNo.length==0)
{ 
document.getElementById(ctlName).innerHTML=""
return
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}        
var url="db/cli/shared/getresidentname.asp"
url=url+"?RegNo="+RegNo
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged(ctlName)
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
} 

function stateChanged(ctlName) 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
   { 
   document.getElementById(ctlName).innerHTML=xmlHttp.responseText 
   } 
} 

function GetXmlHttpObject()
{ 
var objXMLHttp=null
if (window.XMLHttpRequest)
  {
  objXMLHttp=new XMLHttpRequest()
  }
else if (window.ActiveXObject)
  {
  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
return objXMLHttp
}

the body code looks like this,

Code:
<body>
<form name="form1" id="form1"> 
First Name:
<input type="text" id="txt1" onBlur="showName(this.value,txtName1)">
</form>
<p>First Name: <span id="txtName1"></span></p>
</body>

But I get a "Type Mismatch" error on the,

Code:
xmlHttp.onreadystatechange=stateChanged(ctlName)

line. I am not sure where I am going wrong, but as I said I am very new to javascript/AJAX. Anyone know what I should do?

Thanks,
Drew
 
Fixed it, here is the correct code,

Code:
function showName(RegNo,ctlName){
     if (RegNo.length==0){ 
          document.getElementById(ctlName).innerHTML=""
          return
     }
     var xmlHttp = GetXmlHttpObject();
     var url = "db/cli/shared/AJAX_getresidentname.asp";
     url += "?RegNo="+RegNo;
     url += "&sid="+Math.random();
     
     if (!xmlHttp){
          alert ("Browser does not support HTTP Request")
          return
     }
     xmlHttp.onreadystatechange=function(){
     if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
            document.getElementById(ctlName).innerHTML=xmlHttp.responseText;
        }
     };
     xmlHttp.open("GET", url, true);
     xmlHttp.send(null);
}

function GetXmlHttpObject(){ 
var objXMLHttp=null;

     if (window.XMLHttpRequest){
          objXMLHttp=new XMLHttpRequest();
     }else if (window.ActiveXObject){
          objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
     return objXMLHttp;
}

I call the code with this,

Code:
<input type="text" id="txt1" onBlur="showName(this.value,'txtHint')">

Hope this helps someone else...

Thanks,
Drew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top