一个投票系统的源程序(coveryourasp.com)

SurveyDescr.asp

 1   
 2// ============================================   
 3// NOTE: all source code downloaded from CoverYourASP was written by   
 4// James Shaw (unless stated otherwise), and is copyright (c) 2000 by   
 5// James Shaw. You may use this source code on your web sites, but   
 6// please don't publish or distribute in any way.   
 7//   
 8// I would appreciate an HTML comment in any code you use, i.e.   
 9//
10<!-- portions (c) [email protected]>
11// (see Footer(), documented in SSI.asp for details on how to do this)   
12//   
13//

<shameless plug="">
// Please contact me to discuss any ASP contract work you may have.
// </shameless>

 1// ============================================   
 2  
 3// output relevant meta tags   
 4Init( "Survey your readers" );   
 5  
 6// output common top of page   
 7Header( 'Survey your readers' );   
 8  
 9// output page content   
10Content ( );   
11  
12// output common bottom of page   
13Footer( );   
14  
15// ============================================   
16// the content of this page - every page has a function 'Content' that   
17// is called above.   
18// ============================================   
19function Content ( )   
20{   
21Out ( '

<td colspan="2" valign="top" width="80%">' );

// if the survey hasnt been submitted yet...
if ( !Request.Form.Count )
{
//...display some blah, blah
Out ( 'Finally, surveys come to CoverYourASP! I've been wanting to ask you guys and gals
questions for a long time, and now I can. It's up to you if you want to answer of course!' );

Out ( '<p>Of course, the real benefit to you is that if you tell me what you like I'll probably
provide it. If you send in your <a href="Donate.asp">donations</a> the probability increases rather
dramatically!' );

Out ( '<p>Take the example survey below if you have the time and inclination. I plan to post more
in a special survey category, and start offering incentives to take them.' );

Out ( '<p>Afterwards, look at the code. I think you'll be surprised how simple it is to create
surveys with this code. This page has one function call in it, with just one parameter - the name of the
survey! All questions, answers and results are stored in the database.' );
}

// show the survey, or process it's input
ProcessSurvey ( 'Who are you and what do you think?' );

if ( !Request.Form.Count )
Out ( '<p><a href="ShowSource.asp?page=SurveyDescr"><img align="right" border="0" src="images/source.gif"/></a>Please submit the survey first before looking at the source code - this link is on the result
page too!' );
else
Out ( '<p><center><a href="ShowSource.asp?page=SurveyDescr"><img border="0" src="images/source.gif"/></a></center>' );

Out ( '</p></p></p></p></p></td>

1' );   
2Out ( '

<td valign="top" width="20%">' );

// show rotating banners
ShowBanners ( 4 );

Out ( '</td>

1' );   
2}   

utils/Survey.asp

 1   
 2// ============================================   
 3// NOTE: all source code downloaded from CoverYourASP was written by   
 4// James Shaw (unless stated otherwise), and is copyright (c) 2000 by   
 5// James Shaw. You may use this source code on your web sites, but   
 6// please don't publish or distribute in any way.   
 7//   
 8// I would appreciate an HTML comment in any code you use, i.e.   
 9//
10<!-- portions (c) [email protected]>
11// (see Footer(), documented in SSI.asp for details on how to do this)   
12//   
13//

<shameless plug="">
// Please contact me to discuss any ASP contract work you may have.
// </shameless>

 1// ============================================   
 2  
 3// ============================================   
 4// display or process the named survey   
 5// ============================================   
 6function ProcessSurvey ( sName )   
 7{   
 8// has the survey form been submitted?   
 9if ( Request.Form.Count )   
10{   
11// connect to the database   
12DBInitConnection ( );   
13  
14var sSurvey = "" + Request.Form ( "Survey" );   
15  
16// only update the survey when no cookie   
17if ( "" == Request.Cookies ( sSurvey ) )   
18{   
19// get the data from the form and update the database   
20// use an enumerator to get name and value   
21var e = new Enumerator ( Request.Form );   
22  
23while ( !e.atEnd ( ) )   
24{   
25var oItem = e.item();   
26  
27// increment the current number of times this answer has been chosen   
28oConnection.Execute ( 'UPDATE SurveyAnswers SET Hits=Hits+1 WHERE Question="' + oItem + '" AND   
29Answer="' + Request.Form ( oItem ) + '";' );   
30  
31e.moveNext ( );   
32}   
33  
34// note that setting cookie here assumes we are buffering   
35// the Reponse.Writes - cookies must be set before any   
36// HTML is sent to the client   
37Response.Cookies ( sSurvey ) = "1";   
38  
39// I'm not setting the 'expires' on the cookie, so it'll go   
40// away when the browser is closed. I just wanted to stop   
41// the survey incrementing if the page refreshed.   
42}   
43  
44// now display all the answers to the survey   
45Out ( '

<p>Thanks for taking part in our "' + sSurvey + '" survey! The answers that everyone has
given so far are shown below:' );

// the last question we displayed
var sLast = "";

// get all the selected answers, sorted by question and hits
DBGetRecords ( 'SELECT SurveyAnswers.Question,Answer,Hits FROM SurveyAnswers INNER JOIN
SurveyQuestions ON SurveyQuestions.Question=SurveyAnswers.Question WHERE Survey="' + sSurvey + '" AND
Hits&gt;0 ORDER BY SurveyAnswers.Question,Hits DESC;' );

var fScale;

while ( !oRecordSet.EOF )
{
// display question when it changes
var sIntQuestion = "" + oRecordSet ( 0 );

// slice off chars used for sorting
var sQuestion = sIntQuestion.slice ( 2 );

// get answer
var sIntAnswer = "" + oRecordSet ( 1 );

// slice off chars used for sorting
var sAnswer = ExpandMacros ( sIntAnswer.slice ( 2 ) );

var nReaders = oRecordSet ( 2 ) - 0;

if ( sQuestion != sLast )
{
Out ( '<h5>' + sQuestion + '</h5>' );
sLast = sQuestion;

Out ( '<font color="red">"' + sAnswer + '" was the top answer (' + nReaders + ' readers)
</font><br/>' );

fScale = 300.0 / nReaders;
}
else
{
Out ( '"' + sAnswer + '" was chosen by ' + nReaders + ' readers<br/>' );
}

Out ( '<img height="8" src="images/Dot.gif" width="' + (nReaders * fScale) + '"/><br/>' );

oRecordSet.MoveNext ( );
}

// release the connection ASAP
DBReleaseConnection ( );
}
else
{
// some initial instructions
Out ( '<p>There aren't any important instructions when answering these questions - except you
don't have to answer any. All are optional - if you don't like a question, or none of the answers are
relevant, just move onto the next one!' );

// connect to the database
DBInitConnection ( );

// get the questions from the database
DBGetRecords ( 'SELECT Question FROM SurveyQuestions WHERE Survey="' + sName + '" ORDER BY
Question;' );

if ( oRecordSet.EOF )
{
Out ( 'No questions were found for survey "' + sName + '"<p>' );
return;
}

// store the questions in an array
var sIntQuestions = new Array;
var nQuestions = 0;

while ( !oRecordSet.EOF )
{
sIntQuestions [ nQuestions++ ] = "" + oRecordSet ( 0 );
oRecordSet.MoveNext ( );
}

Out ( '<form action="' + Request.ServerVariables ( 'SCRIPT_NAME' ) + '" method="post">' );

// some hidden fields to pass data through to results page
Out ( '<input name="Survey" type="hidden" value="' + sName + '"/>' );

// now loop through the questions
for ( var nQuestion=0; nQuestion<nquestions; '<h5="" 'select="" (="" )="" );="" 2="" [="" ];="" answer,answertype="" answer;'="" answers="" by="" chars="" database="" dbgetrecords="" for="" from="" get="" nquestion="" nquestion++="" off="" order="" out="" question="' + sIntQuestion + '" sintquestion="sIntQuestions" slice="" sorting="" squestion="sIntQuestion.slice" surveyanswers="" the="" used="" var="" where="" {="">' + sQuestion + '' );

while ( !oRecordSet.EOF )
{
// get the answer
var sIntAnswer = "" + oRecordSet ( 0 );

// slice off chars used for sorting
var sAnswer = ExpandMacros ( sIntAnswer.slice ( 2 ) );

var sAnswerType= "" + oRecordSet ( 1 );

switch ( sAnswerType )
{
case 'radio':
Out ( '<input name="' + sIntQuestion + '" type="radio" value="' + sIntAnswer + '"/> ' +
sAnswer );
break;

default:
break;
}

Out ( '<br/>' );

// get next answer
oRecordSet.MoveNext ( );
}
}

Out ( '<p><input type="submit" value="Submit answers"/>' );
Out ( '</p></nquestions;></form>' );

// release the connection ASAP
DBReleaseConnection ( );
}
}

// ============================================
// add links to text where *? macros are found, e.g. *d expands to
// <a href="Donate.asp">Send a donation!</a>
// NOTE: currently assumes expansions are always at end of line
// ============================================
function ExpandMacros ( sText )
{
var sMacros = new Array (
'**',
'*d'
);

var sExpansions = new Array (
'<a href="Contact.asp" target="CYAEXternal">You need to send me feedback!</a>',
'<a href="Donate.asp" target="CYAEXternal">send a donation!</a>'
);

for ( var i=0; i<smacros.length; !="nPos" (="" (c)="" (unless="" )="" );="" +="" -1="" 0,="" 2000="" <!--="" =="//" [="" ]="" ];="" ```="" all="" an="" and="" any="" appreciate="" break;="" but="" by="" code="" comment="" copyright="" coveryourasp="" database.asp="" distribute="" don't="" downloaded="" from="" html="" i="" i++="" i.e.="" if="" in="" is="" james="" [email protected]="" may="" note:="" npos="" on="" or="" otherwise),="" please="" portions="" publish="" return="" sexpansions="" shaw="" shaw.="" sites,="" smacros="" source="" stated="" stext="sText.slice" stext;="" this="" use="" use,="" utils="" var="" was="" way.="" web="" would="" written="" you="" your="" {="" }="">
// (see Footer(), documented in SSI.asp for details on how to do this)
//
//
// Please contact me to discuss any ASP contract work you may have.
//

// ============================================

// globals
var oConnection;
var oRecordSet;

// enums

// Connection.State and Recordset.State property
var adStateClosed = 0; // the object is closed.
var adStateOpen = 1; // the object is open.
var adStateConnecting = 2; // the object is connecting.
var adStateExecuting = 4; // the object is executing a command.
var adStateFetching = 8; // the rows of the object are being fetched.

// Recordset.Cursor property
var adOpenUnspecified = -1; // does not specify the type of cursor.
var adOpenForwardOnly = 0; // (default) a forward-only cursor, i.e. you get only one pass thru the data!
var adOpenKeyset = 1; // can go in any direction, and as a bonus you'll see changes other users
make. EXPENSIVE!
var adOpenDynamic = 2; // as Keyset, but also you can see additions/deletions other users make.
EXPENSIVE!
var adOpenStatic = 3; // can go in any direction, but read-only.

// Recordset.LockType property
var adLockUnspecified = -1; // does not specify a type of lock.
var adLockReadOnly = 1; // (default) guess!
var adLockPessimistic = 2; // guaranteed to work
var adLockOptimistic = 3; // records locked only when you call Update. fingers crossed
var adLockBatchOptimistic = 4;// required for batch update mode

// ============================================
// example usage:
// DBInitConnection ( );
//
// DBGetRecords ( "SELECT * FROM Somewhere" );
//
// ...use oRecordSet
//
// DBReleaseRecords ( ); // optional step
//
// DBGetRecords ( "SELECT * FROM SomewhereElse" );
//
// ...use oRecordSet
//
// DBReleaseRecords ( ); // optional step
//
// DBReleaseConnection ( );
// ============================================

// ============================================
// initializes database variables for first use on page - leave it to the
// last possible second before calling this function
// ============================================
function DBInitConnection ( )
{
// don't open it again if already opened!
if ( oConnection != undefined )
return;

// you can open Recordset objects without a Connection object, but
// it's far less efficient if you are opening multiple Recordsets.
//
// if you don't create a Connection object ADO creates a new one for
// each new Recordset.Open, even if you use the same connection string.
oConnection = Server.CreateObject( 'ADODB.Connection' );

// open the database - use MapPath to make relative path into physical path
// NOTE: keep your database path a secret - nasty people are everywhere!
// 2. change the 4.0 to 3.51 when using Access 97
oConnection.Open( 'Provider=Microsoft.Jet.' + sDBDriver + '; Data Source=' + Server.MapPath (
sDBPath ) );

// create a Recordset
oRecordSet = Server.CreateObject( 'ADODB.Recordset' );
}

// ============================================
// tidies up after DBInitConnection
// ============================================
function DBReleaseConnection ( )
{
// don't release the connection if not connected!
if ( oConnection == undefined )
return;

// close and delete the Recordset object
DBReleaseRecords ( );

oRecordSet = undefined;

// Don't call Close if the Recordset failed to Open properly, i.e. its
// State is still adStateClosed (0)
if ( oConnection.State != adStateClosed )
oConnection.Close();

oConnection = undefined;
}

// ============================================
// executes the passed in SQL statement and returns a read-only
// forward-only oRecordSet object
// ============================================
function DBGetRecords ( sSQL )
{
// if the Recordset is already open, close it
DBReleaseRecords ( );

// we could use oRecordSet = oConnection.Execute( sSQL ) here
// but then we will always get back a read-only, forward-only cursor.
// (admittedly this is the most used type, but still)

// use oRecordSet.Open and we have far more control. For details
// read the definitions of the enums at the top of this file.

// remember that this can fail if passed garbage, and hence the
// Recordset will remain closed, State == adStateClosed
oRecordSet.Open ( sSQL, oConnection, adOpenForwardOnly, adLockReadOnly );
}

// ============================================
// tidies up after DBGetRecords
// ============================================
function DBReleaseRecords ( )
{
// when you have finished with an open Recordset object, call the
// Close method to release its resources. You can call Open again.

// Don't call Close if the Recordset failed to Open properly, i.e. its
// State is still adStateClosed
if ( oRecordSet != undefined && oRecordSet.State != adStateClosed )
oRecordSet.Close();
}

Published At
Categories with Web编程
Tagged with
comments powered by Disqus