SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : XML-Based Survey Server : Saving Survey Results

Saving an individuals vote is very simple. The survey.inc file contains a function, voteSurvey(), that takes the survey ID and user's vote, and saves the updated results. This function returns an HTML-based bar chart of the results.

function voteSurvey(sID,sVote)
 Dim findNode, result, rootPath, source, resultStyle
 if sVote<>"" then
  set source = application("survey")
  set resultStyle = application("style").selectSingleNode("xsl:stylesheet/render[@id=""results""]")
  set findNode = source.selectSingleNode("surveylist/survey[id[. = """ & sID & """]]/answer[.=""" & sVote & """]")
  if not findNode is nothing then
   findNode.setAttribute "votes", findNode.getAttribute("votes") + 1
   set findNode = findNode.parentNode
   findNode.setAttribute "voters", findNode.getAttribute("voters") + 1
   rootPath = server.mappath(".") & "\src\"
   saveSurveys source
   result = findNode.transformNode(resultStyle)
  else
   result="ERROR - Invalid Choice"
  end if
 end if
 voteSurvey = result
end function

This function has similar queries to the getSurvey() function except the initial query against the surveys returns the matching answer element. Once this node is retrieved, we update the number of votes and the total votes and save the file back to disk. After saving, we use the "results" rendering element to generate a bar chart.

<render id="results">
<TABLE BORDER="1">
 <TR><TD ALIGN="CENTER"> 
 <P><xsl:value-of select="question"/></P>
 Total Voters: <xsl:value-of select="@voters"/>
 <FORM>
  <INPUT TYPE="HIDDEN" NAME="ID">
  <xsl:attribute name="VALUE"><xsl:value-of select="id"/></xsl:attribute>
  </INPUT>
  <TABLE ALIGN="center">
  <xsl:for-each select="answer">
   <TR><TD VALIGN="middle">
   <TABLE CELLSPACING="0" CELLPADDING="0" HEIGHT="5">
   <TR><TD BGCOLOR="blue">
   <xsl:attribute name="WIDTH">
     <xsl:eval>Math.round(this.getAttribute("votes")/this.parentNode.getAttribute("voters") * 100)</xsl:eval>
   </xsl:attribute>
   <IMG SRC="/gifs/s.gif" HEIGHT="5">
   <xsl:attribute name="WIDTH">
     <xsl:eval>Math.round(this.getAttribute("votes")/this.parentNode.getAttribute("voters") * 100)</xsl:eval>
   </xsl:attribute>
   </IMG>
   </TD><TD>
   <SMALL>
   (<xsl:eval>Math.round(this.getAttribute("votes")/this.parentNode.getAttribute("voters") * 100)</xsl:eval>%)
   </SMALL>
   </TD></TR></TABLE>
   </TD><TD><xsl:value-of/></TD></TR>
  </xsl:for-each>
  </TABLE>
 </FORM>
 </TD></TR>
 <TR><TD ALIGN="right">
  <FONT FACE="verdana,geneva,arial">
    <SMALL><SMALL>
      Survey Server, Copyright 1999, 
      <A HREF="http://www.siteexperts.com">SiteExperts.com</A>
    </SMALL></SMALL>
  </FONT>
 </TD></TR>
</TABLE>
</render>

To generate the bar chart, we generate an HTML table and use colored table cells to create the chart. This is the same technique presented in our HTML Bar Chart article.

If you have been following the code closely, you may have noticed that we never opened the XML or XSL files but rather are retrieving the XML objects from the global application object. If you look back at the original getSurvey() function you will find a small if-then statement where we test whether the surveys have been loaded:

  if isempty(application("survey")) then
    loadSurveys
  end if

This statement checks whether the surveys have been loaded. If the survey property on the application object is empty, then the surveys are not yet loaded and the loadSurveys function is called:

function loadSurveys() 
  Dim source, style, rootPath
  rootPath = server.mappath(".") & "\src\"
  set source = Server.CreateObject("Microsoft.FreeThreadedXMLDOM")
  source.async = false
  source.load rootPath & "surveys.xml"

  set style = Server.CreateObject("Microsoft.FreeThreadedXMLDOM")
  style.async = false
  style.load rootPath & "surveys.xsl"

  application.lock
  set application("survey") = source
  set application("style") = style
  application.unlock
end function

This function loads the XML and XSL file from disk and caches them in the global application object. You have now seen how the surveys are generated and rendered. To make the Survey Server more usable we created a Survey Manager Page. This page allows you to edit and delete existing surveys. Next we show you how we used the DOM to implemented the Survey Manager page.