?
Solved

Using VBScript variables to affect HTML

Posted on 1998-10-23
2
Medium Priority
?
228 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 300 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…

649 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