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

Inside Technique : XML-Based Survey Server : Editing the Survey List

The Survey Editor supports three operations, deleting, editing, and creating surveys. First we show you how a survey is deleted. The code to delete a survey is very straightforward. We just remove the nodes representing the survey from the document. This is accomplished in very few lines of script. First, you query the document for the survey node, you then remove this node from the survey list, and finally you just write the survey file back to disk:

function deletePoll(sID)
  Dim findNode
  set findNode = getPoll(sID)
  if findNode is nothing then
    response.write("<B>ERROR - Survey not found</B>")
  else
    findNode.parentNode.removeChild(findNode)
    saveSurveys findNode.ownerDocument
  end if
end function

Saving a Survey
The last operation is saving a survey. This code is used for saving new surveys as well as existing ones.

function saveSurvey(oRequest)
  dim i,iVotes,iVote,oNode, oSurvey, oQuestion, oQuestionText
  dim sID,oRoot,oVotersAttribute,oAnswer,oVotesAttribute,oAnswerText
  dim oID, oIDText
	
  set oNode = nothing
  set oRoot = application("survey")
  set oSurvey = oRoot.createNode(1,"survey","")
  set oID = oRoot.createNode(1,"id","")
  set oQuestion = oRoot.createNode(1,"question","")
  set oQuestionText = oRoot.createTextNode(oRequest.form("question"))
  oSurvey.appendChild(oID)
  oQuestion.appendChild(oQuestionText)
  oSurvey.appendChild(oQuestion)
  for i=0 to numAnswers-1
    if Trim(oRequest.form("answer" & i))<>"" then
      iVote=oRequest.form("votes" & i)
      set oAnswer = oRoot.createNode(1,"answer","")
      set oAnswerText = oRoot.createTextNode(oRequest.form("answer" & i))			
      set oVotesAttribute = oRoot.createAttribute("votes")
      oAnswer.appendChild(oAnswerText)
      oSurvey.appendChild(oAnswer)
      if isnumeric(iVote) then 
        oVotesAttribute.nodeValue = iVote
        iVotes = iVotes + cInt(iVote)
      else
        oVotesAttribute.nodeValue = 0
      end if
      oAnswer.attributes.setNamedItem(oVotesAttribute)
    end if
  next
  set oVotersAttribute = oRoot.createAttribute("voters")
  oVotersAttribute.nodeValue = iVotes
  oSurvey.attributes.setNamedItem(oVotersAttribute)
  sID = oRequest.form("id")
  if sID<>"" then
    ' Replace existing survey
    set oNode = getSurveyFromID(sID)
    oNode.parentNode.replaceChild oSurvey,oNode
  else
    ' Add new survey
    Set oNode = oRoot.selectSingleNode("surveylist")
    sID = oNode.lastChild.selectSingleNode("id").text
    sID = cInt(sID) + 1		
    oNode.appendChild(oSurvey)
  end if
  set oIDText = oRoot.createTextNode(sID)
  oID.appendChild(oIDText)
  saveSurveys oRoot
end function

This function is actually fairly simple. It constructs an individual survey node by node and inserts it back into the XML document. While this may seem tedious it is a very straightforward and easy operation. We recommend you read our Content Server article for an explanation on how these methods works.

Last, we finish with a download of the sample files and how to try them on your server.