Solved

Using VBScript variables to affect HTML

Posted on 1998-10-23
2
201 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
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Showing your events from Google Calendar in Google Maps Why? I travel all week and I thought it would be ideal if staff in office knew where I was based on my calendar. (OK real reason: my son wanted to see where I would be working, and I thoug…
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 tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now