1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

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

Discussion in 'Software Development' started by dekesc, Mar 23, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. dekesc

    dekesc Thread Starter

    Joined:
    Aug 4, 2010
    Messages:
    8
    Problem: I'm trying to retrieve a client-side <form> 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 <form> 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 <input> the Form is as follows...
    ==========================
    <form name="addform" method="POST" action="https://toMyWebserver/CBC-CareMinistry/RELM/do_DB_listings_secure.asp?">
    <Table cellspacing="0" cellpadding="0" bgcolor="#ffffff" width="760" BORDER="2">
    <tr>
    <td width="340" height="30" align="right" bgcolor="#FFFFCC" >
    Search on Last Name</td>
    <td height="40" bgcolor="#FFFFCC" >
    <input type="text" name="searchCareContactLastName" id="searchCareContactLastName" value="" size="20">
    </td>
    <td bgcolor="#FFFFCC" >
    <input type="button" name="nameGo" id="nameGo" value="Go" size="10" onclick ="return prepop()"> </td>
    </tr>
    </table>
    ===========================
    The SQL table defines multiple columns each of which is of type "varchar(n)" with the column of interest being: care_Contact_Last_Name.
     
  2. allnodcoms

    allnodcoms

    Joined:
    Jun 30, 2007
    Messages:
    613
    I know absolutely nothing about ASP.NET, but in your HTML the field is id'd as "searchCareContactLastName", yet in your script you have:
    Code:
    strCCLastNameValue = document.getElementById("careContactLastName").value 'THIS IS THE LINE THAT IS GIVING ME PROBLEMS
    It's missing the "search" prefix.

    Danny
     
  3. dekesc

    dekesc Thread Starter

    Joined:
    Aug 4, 2010
    Messages:
    8
    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)
    <font face="Arial" size=2>
    <p>Microsoft VBScript runtime </font> <font face="Arial" size=2>error '800a01f4'</font>
    <p>
    <font face="Arial" size=2>Variable is undefined: 'document'</font>
    <p>
    <font face="Arial" size=2>/CBC-CareMinistry/RELM/admin_listings_mgr_secure_WIP1.asp</font><font face="Arial" size=2>, line 184</font>
    ==================
     
  4. allnodcoms

    allnodcoms

    Joined:
    Jun 30, 2007
    Messages:
    613
    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...
    Your problem here is this:
    Code:
    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
     
  5. dekesc

    dekesc Thread Starter

    Joined:
    Aug 4, 2010
    Messages:
    8
    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
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1046333

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice