PDA

View Full Version : Drop down list...it may be a daft question, but...


manxman
04-29-2004, 12:13 AM
This may be a daft question, but I have written a form that asks the user to choose an option from a series of drop down lists such as:

<FORM METHOD="POST" action="newsafety.asp" webbot-action="--WEBBOT-SELF--">
<input TYPE="hidden" NAME="VTI-GROUP" VALUE="0"><P>
Choose one of the following options:</P>
<BLOCKQUOTE>
<p>Is there a suitable updated and reviewed Health & Safety Plan and does it
refer to MCG Safety Strategy and methods of consultation?</p>
<p><select NAME="1" size="1">
<option SELECTED value="0">No H&S plan at all
<option value="1">Significant amount of H&S plan incomplete
<option value="2">H&S plan in place but not reviewed or small amount incomplete
<option value="3">H&S plan in place, reviewed on a quarterly basis
<option value="4">Plan in place, reviewed monthly
</select><br>
</p>

The answers are then stored in a database and the final confirmation field will display the questions and selected answers together as a certificate.

It is intended to be used as an online site tool for conducting safety audits around our water treatment works (the current test version is here (without final confirmation page) http://www.hmballiance.com/newsafety/newsafety.asp ).

The above all works fine, however, I would like to display a running score on the page as the user works through the questions. I am relatively new to ASP so it might something that is really simple, however I have searched for days to find a solution, to no avail.

The closest I have come is adding <% score = score + 1 %> etc for each option, and <% response.write "Your score is (score) %> at the end, however this just seems to total the score as if every option has been selected (i.e. score = 10 for the above example).

Is there a solution?

glenngv
04-29-2004, 04:09 AM
<%
dim i, score
score = 0
For i = 1 to 20
score = score + CInt(Request.Form(i))
Next
Response.Write "Your score is " & score
%>

You name your selects as plain numbers (1-20)
I recommend you to use more descriptive names like question1, question2, ...question20.
Then change this line:

score = score + CInt(Request.Form(i))

to:

score = score + CInt(Request.Form("question" & i))

manxman
04-29-2004, 11:16 AM
Cheers for that, but I must be doing something wrong, as the score doesn't seem to change as you choose your answers.

I wondered if there is an ASP equivalent to the onchange function in javascript, whereby as the user selects an answer, the score variable automatically changes, giving a running score as they work through the page.

glenngv
04-30-2004, 04:47 AM
function calculate(f){
var total=0;
for (var i=1;i<=20;i++){
total+=parseInt(f.elements['question'+i].options[f.elements['question'+i].selectedIndex].value, 10);
}
document.getElementById('total').innerHTML=total;
}
...
<select NAME="question1" size="1" onchange="calculate(this.form)">
...do the same for other selects
...
Your score is <span id="total">0</span>