Solved

Using VBScript variables to affect HTML

Posted on 1998-10-23
2
217 Views
Last Modified: 2010-04-09
Why do I not seem to be able to access in the <BODY> of the page, VBScript variable values (initialized and assigned values from a temporary cookie in the <HEAD>)?  I want to read cookie values and use that data to create an ADO recordset from an Access database, and in turn use the returned database info to determine which GIFs get loaded by the HTML page, and I'd like to do all this when the page is first loaded.

Some code parts....
<HEAD>
<SCRIPT LANGUAGE="vbscript">
Alert document.cookie  'temporary - for diagnostics
Dim ImagesOrdered()
Dim numImagesOrdered
Dim strCookie
If InStr(Document.Cookie, "=ordered") = 0 Then
      'the cookie has no images ordered
      Alert ("Your cookie is empty!")
Else
      'the cookie has images ordered
      DigestCookie
End if
Sub DigestCookie
      Dim strCookie
      Dim Cookie(1000)
      strCookie = Document.Cookie
      'chop cookie into pieces
      i = 0
      numCookies = 0
      Do While i <= 999
            cookieLength = Len(strCookie)
            endCookie = InStr(strCookie, ";") - 1
            If endCookie = -1 Then
                  i = 1000                              'i.e. exit loop
            Else
                  'each pair of cookie variable names and values is placed in an array element
                  'cookie(0) is reserved for ImageCartCapacity
                  'cookie(1) is reserved for numImagesOrdered (i.e. num of values = "ordered")
                  Cookie(i) = Left(strCookie, endCookie)
                  strCookie = Right(strCookie, cookieLength - endCookie - 1)
                  i = i + 1
                  numCookies = numCookies + 1
            End if
      Loop
      'determine Image Cart Capacity
      If InStr(Cookie(0), "ImageCartCapacity") = 0 Then
            Alert ("Your ImageCart is empty!  You can only review your order if you have ordered some images during this visit.")
      Else
            cookieLength = Len(Cookie(0))
            startposnValue = InStr(Cookie(0), "=")
            ImageCartCapacity = Right(Cookie(0), cookieLength - startposnValue)
                                          Alert ("ImageCartCapacity=" & ImageCartCapacity)
      End if
      'determine number of cookies for ordered images only
      If InStr(Cookie(1), "numImagesOrdered") = 0 Then
            Alert ("Your ImageCart is empty!  You can only review your order if you have ordered some images during this visit.")
      Else
            cookieLength = Len(Cookie(1))
            startposnValue = InStr(Cookie(1), "=")
            numImagesOrdered = Right(Cookie(1), cookieLength - startposnValue)
                                          Alert ("numImagesOrdered=" & numImagesOrdered)
      End if
      'reset arrays to proper size
      Redim Preserve Cookies(numCookies)
      Redim ImagesOrdered(numImagesOrdered)
      'populate ImagesOrdered array from relevant cookies
      i = 0
      j = 0
      Do While i <= numCookies
            If InStr(Cookie(i), "not ordered") = 0 And InStr(Cookie(i), "ordered") <> 0 Then
                  cookieLength = Len(Cookie(i))
                  startposnValue = InStr(Cookie(i), "=")
                  j = j + 1
                  ImagesOrdered(j) = Mid(Cookie(i), 7, cookieLength - 14)
            End if
            i = i + 1
      Loop
End function
...
</SCRIPT>
</HEAD>

<BODY>
...
create a strSQL out of VBScript variables, down here in the body, and execute it to get a recordset called rsPhotos
OH AND BY THE WAY, THIS ISN'T WORKING
...

<TABLE>
<% For i = 1 to numImagesOrdered %>  
//THAT DOESN'T WORK - WHY NOT?
<tr>      <td>
<INPUT TYPE="radio" NAME="ordered1" VALUE="1">Yes<BR>
<INPUT TYPE="radio" NAME="ordered1" VALUE="0">No</td>
<td ALIGN="center">
      nnn</td>
<td ALIGN="center">
      colour swatch</td>
<td ALIGN="center">
<IMG SRC="<%response.write(rsPhotos("Image_ID"))%>.gif"
      ALIGN="center" BORDER="0" WIDTH="25%"></td>
<td ALIGN="left">
<FONT FACE="Arial, Helvetica, sans-serif"><strong>
        Description</FONT></strong></td>
<td ALIGN="CENTER">
<FONT FACE="Arial, Helvetica, sans-serif"><strong>
      Legal Considerations</FONT></strong></td>
<td ALIGN="CENTER">
<FONT FACE="Arial, Helvetica, sans-serif"><strong>
      Image Format(s)</FONT></strong></td></tr>
<% Next %>
</table>

I expect the problem has something to do with the sequence in which things happen, but I don't know that sequence, or how to change it or take advantage of it.  Help?
0
Comment
Question by:sknippel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 28

Accepted Solution

by:
sybe earned 100 total points
ID: 1838978
You are mixing client-side script with server-side script. ASP is processed serverside, for example this:

<% For i = 1 to numImagesOrdered %>

And this is processed before any code is send to the browser.
the variable numImagesOrdered is not initialized server-side (so presumed zero).
The solution is to use serverside script to read the cookie.

<%
var = Request.Cookies("name")
%>

0
 

Author Comment

by:sknippel
ID: 1838979
Thanks greatly.  One last question:
Is it possible to access cookie variables without knowing their names?  That is, can one simply specify something like:

<% var=Request.Cookies(2) %>

to get the second (or third, assuming 0 counts as the first one) cookie variable's value?
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question