PDA

View Full Version : survey adding script



Reding
02-09-2006, 04:58 PM
Hi everybody,

i'm new to all this and hoping you could help me out on something. i'm looking to setup a survey with 55 questions, each of them with a slot to fill in a ratingnumber from 0 to 4. when all rating numbers are filled in, the user should be able to see the total (all rating numbers added).

Does anybody know where i can find a script like that or who could helpwith this simple script?

thank you, Ed

Twey
02-09-2006, 05:48 PM
In Javascript? Certainly.

<form action="" method="">
<table style="border-style:none;border-width:0;">
<tr>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
</tr>
<tr>
<th>Question One:</th>
<td><input type="radio" value="0" name="q1" /></td>
<td><input type="radio" value="1" name="q1" /></td>
<td><input type="radio" value="2" name="q1" /></td>
<td><input type="radio" value="3" name="q1" /></td>
<td><input type="radio" value="4" name="q1" /></td>
</tr>
<tr>
<th>Question Two:</th>
<td><input type="radio" value="0" name="q2" /></td>
<td><input type="radio" value="1" name="q2" /></td>
<td><input type="radio" value="2" name="q2" /></td>
<td><input type="radio" value="3" name="q2" /></td>
<td><input type="radio" value="4" name="q2" /></td>
</tr>
<!-- And so on... -->
</table>
<p id="total" style="height: 1em;"></p>
<input type="button" onclick="
var e = this.form.elements, i, j, total = 0;
for(j=1;e['q' + j];j++)
total += parseInt(e['q' + j].value);
document.getElementById('total').innerHTML = total;
" />
</form>

Reding
02-10-2006, 12:32 PM
Twey,

thank you so much for your script, i'll be using it for sure...

i found a glitch when testing it in dreamwaever, tried to solve it myslef but don't find my way around it.

the zero score appears above the questions instead of above the radio buttons, and also when i select for example 2 and for the next question 3, pressing the button on the bottom shows "NaN" instead of the result of 5

Ed

Twey
02-10-2006, 04:01 PM
You're right, my fault. Corrected code below.
<html>
<head>
<title>Hello</title>
</head>
<body>
<form action="" method="">
<table style="border-style:none;border-width:0;">
<tr>
<td>&nbsp; </td>

<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
</tr>

<tr>
<th>Question One:</th>
<td><input type="radio" value="0" name="q1" /></td>
<td><input type="radio" value="1" name="q1" /></td>
<td><input type="radio" value="2" name="q1" /></td>
<td><input type="radio" value="3" name="q1" /></td>
<td><input type="radio" value="4" name="q1" /></td>
</tr>

<tr>
<th>Question Two:</th>
<td><input type="radio" value="0" name="q2" /></td>
<td><input type="radio" value="1" name="q2" /></td>
<td><input type="radio" value="2" name="q2" /></td>
<td><input type="radio" value="3" name="q2" /></td>
<td><input type="radio" value="4" name="q2" /></td>
</tr>

<!-- And so on... -->
</table>
<p id="total" style="height: 1em;"></p>
<input type="button" onclick="
var e = this.form.elements, j, k, total = 0;
for(j=1;e['q' + j];j++)
for(k=0;e['q' + j][k];k++)
if(e['q' + j][k].checked) {
if(!isNaN(parseInt(e['q' + j][k].value))) total += parseInt(e['q' + j][k].value);
break;
}
document.getElementById('total').innerHTML = total;
" />
</form>
</body>
</html>