# Solved: problem setting server-side variable from client-side sub



## dekesc (Aug 4, 2010)

Problem: I'm trying to retrieve a client-side element value into a server-side variable. The code works if I explicitly set strCCLastNameValue = (a data value in the db table column "care_Contact_Last_Name" such as "Jones"). I hope I have explained the situation adequately. Thanks for any suggestions on resolving this issue.
Here's the code in an asp file...
============================
<script language="VBScript">
Sub prepop()
<% 
Dim dbConn, oRS, sSQL, strCCLastNameValue
strCCLastNameValue = document.getElementById("careContactLastName").value 'THIS IS THE LINE THAT IS GIVING ME PROBLEMS
Set dbConn = Server.CreateObject("ADODB.Connection")
dbConn.Open "Provider=SQLOLEDB;Server=MYServerurl;Database=wpgXXXXX;NetworkLibrary=DBMSSOCN;UID=XXXXX;PWD=YYYYYYYY"
Set oRS = Server.CreateObject("ADODB.Recordset")
oRS.CursorLocation = 3 'adUseClient
With oRS
.ActiveConnection = dbConn
.Source = "SELECT * FROM CareMinistry WHERE care_Contact_Last_Name= '" & strCCLastNameValue & "'"
.CursorType = 0 
.LockType = 3 
.CursorLocation = 3 
.Open
End With
oRS.MoveFirst
%>
'Then fill the with the selected row.
document.addform.entryStatus.value = "<%= oRS("entry_Status") %>"
document.addform.entryCategory.value = "<%= oRS("entry_Category") %>"
document.addform.entryDate.value = "<%= oRS("entry_Date") %>"
document.addform.entryTime.value = "<%= oRS("entry_Time") %>" 
document.addform.careContactLastName.value = "<%= oRS("care_Contact_Last_Name") %>" 
' ...and more fields to fill
<%
' Close objects
%>
End Sub
In the asp file which invokes the above sub on a "onclick" event of the following the Form is as follows...
==========================



Search on Last Name



===========================
The SQL table defines multiple columns each of which is of type "varchar" with the column of interest being: care_Contact_Last_Name.


----------



## allnodcoms (Jun 30, 2007)

I know absolutely nothing about ASP.NET, but in your HTML the field is id'd as "searchCareContactLastName", yet in your script you have:

```
strCCLastNameValue = document.getElementById("careContactLastName").value 'THIS IS THE LINE THAT IS GIVING ME PROBLEMS
```
It's missing the "search" prefix.

Danny


----------



## dekesc (Aug 4, 2010)

Thanks for taking the time to look at this issue. The development environment is classic asp, not asp.net. As for the missing word, 'search' my bad, as I made a mistake in entering ther code. In my HTML editor both have the same element name, searchCareContactLastName.

BTW, I have pasted the code when I run the app in my IE9 browser....

===================
Sub prepop(CCLastName)

Microsoft VBScript runtime error '800a01f4'

Variable is undefined: 'document'


/CBC-CareMinistry/RELM/admin_listings_mgr_secure_WIP1.asp, line 184 
==================


----------



## allnodcoms (Jun 30, 2007)

After a bit of head scratching (and a re-read of your problem) I think I've nailed it down.

The clue was in your original question, and I just didn't spot it...


> Problem: I'm trying to retrieve a client-side element value into a *server-side* variable.


Your problem here is this:

```
document.getElementById(); // <- Javascript
```
JS (unless you're running node) runs client side, not server, so when asp hits 'document' it has no idea what you're talking about and assumes it's an undefined object. 'Document' is one of the root elements of the DOM, but once your script is called that HTML is gone (so even if JS did work, the element you're looking for no longer exists), but the value it contained was sent to your script in the POST array.

And that's where you should be hunting for your variable... 

Danny


----------



## dekesc (Aug 4, 2010)

Thanks for the suggestion. Based on it, what I have decided to do is this:

1. use JavaScript instead of VBScript.
2. Access the corresponding db value 
3. do "server-side writes" to the form elements in the browser.

See any concerns with this approach?

Darold


----------

