Solved

Using VBScript variables to affect HTML

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
File download script VBSCRIPT fail on download PPT files 2 51
Python - desktop use 1 30
Javasctipt 2 24
How would I do a website like this? 5 54
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

808 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