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!

AJAX and ASP Response.Write

Status
Not open for further replies.

dr00bie

Programmer
Feb 19, 2004
108
US
I am using AJAX to return a name from the database so that the user inputting data knows that they input the correct register # (a unique number for each person in the db).

Here is the interface code,

Code:
<label for="RegNo" class="required">First Name:</label>
<input type="text" id="RegNo" onBlur="showName(this.value,'Name1','RegNo1','EPI1')">
<input name="RegNo1" type="hidden" value="">
Name: <span id="Name1" class="boldtext"></span>
<br>
<label for="AggRegNo" class="required">Second Name:</label>
<input type="text" id="AggRegNo" onBlur="showName(this.value,'Name2','RegNo2','EPI2')">
<input name="RegNo2" type="hidden" value="">
Name: <span id="Name2" class="boldtext"></span>
<br>

code for showName function,

Code:
function showName(RegNo,ctlName,ctlName2,ctlName3){
     if (RegNo.length==0){ 
          document.getElementById(ctlName).innerHTML=""
		  document.form1[ctlName2].value = RegNo;
          return
     }
     var xmlHttp = GetXmlHttpObject();
     var url = "//swvtc06/swvtc/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;
			//document.getElementById(ctlName2).Value=RegNo
			document.form1[ctlName2].value = RegNo;
        }
     };
     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;
}

And the ASP page that writes out the name,

Code:
Dim rsName
Dim rsName_numRows

Set rsName = Server.CreateObject("ADODB.Recordset")
rsName.ActiveConnection = MM_CliCore_STRING
rsName.Source = "SELECT RegNo, CliFName, CliMM, CliLName, CliEPI, CliDOB FROM tblClients WHERE CliActive = 1 AND RegNo = " & Request.QueryString("RegNo")
'Response.Write(rsName.Source)
rsName.CursorType = 0
rsName.CursorLocation = 2
rsName.LockType = 1
rsName.Open()

rsName_numRows = 0

If rsName.BOF Or rsName.EOF Then
	Response.Write("No Resident With Register Number Provided")
Else
	rsName.Fields.Item("CliFName").Value & " " & rsName.Fields.Item("CliMM").Value & ". " & rsName.Fields.Item("CliLName").Value)
End If

Now, I would like to use the same function to return a different column from the database (CliEPI) and place it in a hidden textbox.

How can I do this? I tried response.writing the CliEPI out with the name, and then splitting it in javascript, but my javascript knowledge is not too good, and I was unable to sort it out.

Thanks,
Drew
 
I finally figured this out. I decided to use ASP to write out a JSON string, then eval it in the javascript. Here is the updated code,

The ASP page that writes out the data,

Code:
Dim rsName
Dim rsName_numRows

Set rsName = Server.CreateObject("ADODB.Recordset")
rsName.ActiveConnection = MM_CliCore_STRING
rsName.Source = "SELECT RegNo, CliFName, CliMM, CliLName, CliEPI, CliDOB FROM tblClients WHERE CliActive = 1 AND RegNo = " & Request.QueryString("RegNo")
'Response.Write(rsName.Source)
rsName.CursorType = 0
rsName.CursorLocation = 2
rsName.LockType = 1
rsName.Open()

rsName_numRows = 0

If rsName.BOF Or rsName.EOF Then
	Response.Write("No Resident With Register Number Provided")
Else
	Response.Write("{ 'name': '" & rsName.Fields.Item("CliFName").Value & " " & rsName.Fields.Item("CliMM").Value & ". " & rsName.Fields.Item("CliLName").Value & "', 'epi': " & rsName.Fields.Item("CliEPI").Value & " }")
End If

This outputs a string like this, { 'name': 'John R. Doe', 'epi': 1 }

code for showName function

Code:
var info;
	 
function showName(RegNo,ctlName,ctlName2,ctlName3){
     if (RegNo.length==0){ 
          document.getElementById(ctlName).innerHTML=""
		  document.form1[ctlName2].value = RegNo;
          return
     }
	 var xmlHttp = GetXmlHttpObject();
     var url = "//swvtc06/swvtc/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"){ 
			var responseText = xmlHttp.responseText;
            info = eval('(' + responseText + ')');
			
			document.getElementById(ctlName).innerHTML=info.name;
			document.form1[ctlName2].value = RegNo;
			document.form1[ctlName3].Value=info.epi;
        }
     };
     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;
}

This code evals the JSON string written by ASP and then returns the variable "info", you can get at the "variables" in "info" by their name, i.e. info.name, or info.epi.

Hope this helps!
Drew
 
There was a mistake in the code above,

Code:
document.form1[ctlName3].Value=info.epi;

needs to be changed to,

Code:
document.form1[ctlName3].Value = info.epi;

Since I am so new to javascript, I don't have an explanation, but it doesn't work without the "=" having spaces before and after.

Thanks,
Drew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top