Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 16th, 2007, 04:18 AM
Registered User
 
Join Date: Feb 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Change in asp page

Hi Guys

I am no developer but trying to learn a little.
How would i get the results in a page to open up in a new window, and if possible determine the size of this window

Here is the code

 <% @Language="VBScript" %>
<% OPTION EXPLICIT %>
<%
'/************************************************** *********************************
'* *
'* Developed By: Corsair Developments (corsairdev.com) *
'* *
'* Description: Polling Script to provide an interactive poll. *
'* *
'* Date Finished: 07/14/2002 *
'* Date Modified: 07/18/2002 *
'* Copyright: (C) 2002 Corsair Developments *
'* *
'* This script may be freely used. *
'* *
'************************************************* **********************************/

'To reset the results you'll need to go add ?Admin=ResetResults onto the URL and it will automatically
'reset the file that is holding the values. For example:
'http://www.somewebsite.com/ThisFile.asp?Admin=ResetResults

'*********** BEGIN GLOBAL VARIABLES ***********'
'Constants needed to tell FileSystemObject what to do.
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const TriStateUseDefault = -2
Const TriStateTrue = -1
Const TriStateFalse = 0

'change strOptionsArray() to hold the number of options
'change intValuesArray() and intResultsArray() to hold one more then the number of options.
'don't change strBarColors() unless you add more color images for bar graphs.
Dim strOptionsArray(5), intValuesArray(6), intResultsArray(6), strBarColors(9), strFileLocation

'form options
strOptionsArray(0) = "After Pakistan - First !"
strOptionsArray(1) = "Can we beat Aussies ?"
strOptionsArray(2) = "BMT is questionable ?"
strOptionsArray(3) = "Smith is not consistent"
strOptionsArray(4) = "Who cares im looking forward to it !!!!"

'bar graph colors
strBarColors(0) = "red.gif"
strBarColors(1) = "blue.gif"
strBarColors(2) = "green.gif"
strBarColors(3) = "yellow.gif"
strBarColors(4) = "orange.gif"
strBarColors(5) = "aqua.gif"
strBarColors(6) = "purple.gif"
strBarColors(7) = "black.gif"
strBarColors(8) = "gray.gif"

Const intMaxSize = 100 'this is the size in pixels of how big the bar graph can be.

'IMPORTANT: uncomment the string that applies to you (only one strFileLocation should be uncommented)
'must point to a directory that has "write" permissions.

strFileLocation = Server.MapPath("\asp") & "pollResults.txt"


Const strThisFile = "vote.asp" 'name of this file.
Const strImageDir = "vote/" 'name of the directory that holds images relative to this file.
'************ END GLOBAL VARIABLES ************'

Sub VoteForm()
%>
<HTML>
<HEAD>
<TITLE>Voting Poll</TITLE>
<SCRIPT LANGUAGE="JavaScript">
//check to make sure something was selected for submission. Return false, if not, return true and submit if something was.
function errorCheck()
{
    var bFlag = false;
    var objRadio = document.VoteForm.Option;

    //loop through to see if anything was checked.
    for (i = 0; i < <%= UBound(strOptionsArray) %>; i++)
    {
        if (objRadio[i].checked == true)
        {
            bFlag = true;
        }
    }

    if (!bFlag)
    {
        alert("No option was selected.\nMust select an option before submitting.");
    }

    return bFlag;

}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
How well do you think the protea's will do in the World Cup ?:<BR>
<FORM ACTION="<%=strThisFile%>" METHOD="POST" NAME="VoteForm" onSubmit="return errorCheck();">
<%
    'output the options
    Dim counter
    For counter = 0 to (UBound(strOptionsArray) - 1)
        Response.Write "<INPUT TYPE=""RADIO"" NAME=""Option"" VALUE=""" & replace(strOptionsArray(counter), """", "&quot;") & """>" & replace(strOptionsArray(counter), """", "&quot;") & "<BR>" & vbCrLf
    Next
%>&nbsp; <INPUT TYPE="SUBMIT" VALUE="Submit"><P>
</FORM>
<A HREF="<%=strThisFile%>?show=results">View Results</A>
</BODY>
</HTML>
<%
End Sub

Sub ShowResults(oFSO)
    'check to make sure the FileSystemObject passed in IS an Object
    If (IsObject(oFSO)) Then
        'Declare local variables, REMEMBER arrays need to hold one more
        'spot then there are options to hold the total tally
        Dim oFSStream, counter, innerCounter, intPercent, intPercentInteger, intPercentDecimal, strPercent

        'get the file to input the results
        Set oFSStream = oFSO.OpenTextFile(strFileLocation, ForReading)

        'input the results
        For counter = 0 to (UBound(strOptionsArray) - 1)
            intValuesArray(counter) = CInt(replace(oFSStream.ReadLine, vbCrLf, ""))
        Next

        'don't forget we have the total tally to get as well.
        intValuesArray(UBound(strOptionsArray)) = CInt(replace(oFSStream.ReadLine, vbCrLf, ""))

        '**************** EDIT THE LOOK OF THE RESULTS HERE
        'Starting HTML here
        Response.Write "<HTML>" & vbCrLf & "<HEAD>" & vbCrLf & vbTab & "<TITLE>Voting Results</TITLE>" & vbCrLf & "<BODY BGCOLOR=""#FFFFFF"">" & vbCrLf
        Response.Write "<TABLE BORDER=""0"">" & vbCrLf
        'output each option and it's percentage of votes.
        For counter = 0 to (UBound(strOptionsArray) - 1)
            Response.Write vbTab & "<TR>" & vbCrLf & vbTab & "<TD>"

            'This actually outputs to the screen.
            'First we output the name of the option followed by a ":"
            'then we output the percentage of the total votes
            'If you want to change how it is outputed then change this line
            Response.Write strOptionsArray(counter) & ": </TD><TD>" & "<IMG SRC=""" & strImageDir

            'circle through bar graph colors to pick a color to display
            If (counter < UBound(strBarColors)) Then
                innerCounter = counter
                Response.Write strBarColors(innerCounter) & """"
            Else
                innerCounter = counter MOD UBound(strBarColors)
                Response.Write strBarColors(innerCounter) & """"
            End If


            intPercent = CDbl(intValuesArray(counter) / intValuesArray(UBound(strOptionsArray)))
            intPercentInteger = CInt(intPercent * 100)
            If (intPercentInteger > (intPercent * 100)) Then
                intPercentInteger = intPercentInteger - 1
            End If
            intPercentDecimal = CInt(CDbl((intPercent * 100) - intPercentInteger) * 100) 'get the first two decimals
            strPercent = CStr(intPercentInteger & "." & intPercentDecimal)

            Response.Write " BORDER=""0"" ALT=""" & strPercent & "%"" HEIGHT=""20"" WIDTH=""" & CInt((CDbl(intValuesArray(counter) / intValuesArray(UBound(strOptionsArray))) * intMaxSize)) & """>"
            Response.Write " (" &strPercent & "%)<BR>"
            Response.Write "</TD>" & vbCrLf & vbTab & "</TR>" & vbCrLf
        Next
        Response.Write "</TABLE>" & vbCrLf
        'output the total number of votes cast.
        Response.Write "" & vbCrLf
        Response.Write "Total Votes Cast: " & intValuesArray(UBound(strOptionsArray)) & vbCrLf

        'Ending HTML here
        Response.Write vbCrLf & "<P><A HREF=""" & strThisFile & """>Back to Poll</A>" & vbCrLf
        Response.Write vbCrLf & "</BODY>" & vbCrLf & "</HTML>"
        '**************** STOP EDITTING THE LOOK OF THE RESULTS HERE
    Else
        'Write an error.
        'Starting HTML here
        Response.Write "<HTML>" & vbCrLf & "<HEAD>" & vbCrLf & vbTab & "<TITLE>Voting Results</TITLE>" & vbCrLf & "<BODY BGCOLOR=""#FFFFFF"">" & vbCrLf

        'error here.
        Response.Write "<B><EM>Unable to locate results</EM></B><BR>"

        'Ending HTML here
        Response.Write vbCrLf & "</BODY>" & vbCrLf & "</HTML>"
    End If
End Sub

Sub StoreResults(strOption)
    'Declare local variables, REMEMBER arrays need to hold one more
    'spot then there are options to hold the total tally
    Dim oFSO, oFSStream, counter

    'create a FileSystem Object
    Set oFSO = Server.CreateObject("Scripting.FileSystemObject")

    'Check to see if the File doesn't exist, because if it
    'doesn't then we must create the file. Otherwise, we want to
    'read in the old results.
    If (oFSO.FileExists(strFileLocation)) Then
        'open the file for reading.
        Set oFSStream = oFSO.OpenTextFile(strFileLocation, ForReading)
    Else
        'create a default file with 0 tallys
        Set oFSStream = oFSO.CreateTextFile(strFileLocation)
        For counter = 0 to (UBound(strOptionsArray) - 1)
            oFSStream.WriteLine 0
        Next
        oFSStream.WriteLine 0
        'now reopen for reading.
        Set oFSStream = oFSO.OpenTextFile(strFileLocation, ForReading)
    End If

    'Get the old results
    For counter = 0 to (UBound(strOptionsArray) - 1)
        'read in the value from the text file, and convert it to an
        'integer, while replacing any New Line characters encountered.
        intResultsArray(counter) = CInt(replace(oFSStream.ReadLine, vbCrLf, ""))
    Next

    'read in the last line which is the total tally of all the votes.
    'NOTE: remember arrays are referenced at 0, so although we are referencing
    'the spot that you would think is the last option's tally, it is really the
    'spot we reserved for the total, because UBound(strOptionsArray) - 1 is really the last
    'option's tally.
    intResultsArray(UBound(strOptionsArray)) = CInt(replace(oFSStream.ReadLine, vbCrLf, ""))

    'decide which option's tally needs to be updated.
    For counter = 0 to (UBound(strOptionsArray) - 1)
        'if the option selected matches the Option in the array
        'for the value of the variable i then increment the Results array
        If (strOption = strOptionsArray(counter)) Then
            intResultsArray(counter) = intResultsArray(counter) + 1
        End If
    Next

    'increment the total tally
    intResultsArray(UBound(strOptionsArray)) = intResultsArray(UBound(strOptionsArray)) + 1

    'open the file to write.
    Set oFSStream = oFSO.OpenTextFile(strFileLocation, ForWriting)

    'write the new results to the file.
    For counter = 0 to (UBound(strOptionsArray) - 1)
        oFSStream.WriteLine intResultsArray(counter)
    Next

    'write, as the last line, the total
    oFSStream.WriteLine intResultsArray(UBound(strOptionsArray))

    'show the results and pass the FileSystemObject into the sub procedure
    'to save resources so we don't have to reopen a new one.
    call ShowResults(oFSO)

    'destroy unused objects.
    oFSStream.Close
    Set oFSStream = Nothing
    Set oFSO = Nothing
End Sub

'************************************************* ***************
'*************** DECIDE WHAT TO DO WITH PAGE HERE ***************
'************************************************* ***************

'Check to see if there are any Form Submission Variables,
'If there are, update and display the results, otherwise show form.
If (Request.Form("Option") <> "") Then
    call StoreResults(Request.Form("Option"))
 Else If (Request.QueryString("show") = "results") Then
    Dim oFSO
    Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
    call ShowResults(oFSO)

    Set oFSO = Nothing
  Else If (Request.QueryString("Admin") = "ResetResults") Then
     Dim oFSOReset, oFSReset
    Set oFSOReset = Server.CreateObject("Scripting.FileSystemObject")
    Set oFSReset = oFSOReset.CreateTextFile(strFileLocation)

    oFSReset.Close
    Set oFSO = Nothing
    Set oFSReset = Nothing
    Response.Write "Poll Results reset"
Else
    call VoteForm()
  End If
 End If
End If
%>

 
Old February 18th, 2007, 12:34 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I have no idea why you posted all of that code. >< Ok first a few pointers, most of us aren't going to sit here and read through all of that code line by line, we just don't have the time. If you suspect that a portion of your code is suspect for errors, post the relevant parts of it and i am sure we will take a look at it! ^^

Second, about your questions.

Judging from the Author comments of the script, this is designed to work as a polling system, which is a fairly common request of websites but, what I didn't see in your code was any call to a Recordset object so this tells me the results aren't being stored in a database??

I make this assumption with the following reply: the actual poll itself is not being displayed in a popup window, rather it is hosted in a content page somewhere on your site.

With that said when the user checks off an answer to the poll they would hit submit, from there the data would be wrote to a database and their entry would be effectively counted. After the database operations are complete you could write this line of javascript back to the browser:

window.open(); (obviously this is void of all necessary parameters)

which will spawn you a popup window, on this page you could just simply query the database to list the results of the ongoing poll.

Lastly to detect the window size you can use javascript to do that and i THINK the syntax for IE is

document.body.clientHeight;
document.body.clientWidth;

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html

Discussion:
http://p2p.wrox.com/topic.asp?TOPIC_ID=56429





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to dynamically change an HTML page kpnikhil PHP How-To 1 September 7th, 2007 03:53 AM
button command for page change Jonas Access 5 June 6th, 2007 11:39 AM
query result page? CHANGE 1 cedwards Dreamweaver (all versions) 31 July 1st, 2005 12:05 PM
Change Page in iframe qazi_nomi Javascript How-To 10 February 21st, 2005 07:45 AM
Dynamically change html code of a page YiannisF ASP.NET 1.0 and 1.1 Basics 2 July 29th, 2004 01:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.