Check out the LimeSurvey source code on GitHub!

Automatically submit with blank answers

More
5 years 2 months ago #70881 by misterdot
Hi,
I have a question that computes an average (based on previous questions). Since I don't need the user to see the average -- it just gets entered in the DB -- I'm using javascript to press the submit button. If the user leaves any of the questions blank used to compute the average, it won't automatically submit. I've worked around it by putting "please press the submit button" on the page. If they've completed all the questions, that flashes for a second, then it submits. If previous questions are blank, they have to press submit. Is there any way to get submit to fire regardless of if the calculation is successful? Below is my question script:
<font color="white">Average</font> <SCRIPT LANGUAGE="JavaScript">
 
function sum ()
{ answer = ({INSERTANS:87378X2X121}+{INSERTANS:87378X2X122}+{INSERTANS:87378X2X123});
}
 
function average ()
{ sum();
  averageValue = answer / 3;
}
 
var answer, averageValue;
 
sum();
average();
 
$(document).ready(function() {
document.getElementById("answer87378X33X477").value = (averageValue)
document.getElementById('display477').style.display= "none";
document.getElementById('answer87378X33X477').style.display= "none";
});
 
function custom_on_load(){
   document.limesurvey.submit();
}
 
window.setTimeout( 'custom_on_load()',0);
 
</SCRIPT>

Thanks,
Matt

Please Log in to join the conversation.

More
5 years 2 months ago #70891 by tpartner
So let me get this straight...

There is only the hidden question on the page?

Do you want the average computed if there are less that 3 values? In other words, divide the total by 2 if only 2 questions are answered?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
5 years 2 months ago #70896 by misterdot
Correct -- just that single hidden question on the page. And yes, it would be very nice to divide by 2 if only 2 questions were answered. My javascript skills are not up to that task.

Please Log in to join the conversation.

More
5 years 2 months ago - 5 years 2 months ago #70902 by tpartner
Try this:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() { 
 
		// Hide hide the <body> element
		$('body').hide();
 
		// grab the previous answers
		var answer1 = $.trim('{INSERTANS:87378X2X121}');
		var answer2 = $.trim('{INSERTANS:87378X2X122}');
		var answer3 = $.trim('{INSERTANS:87378X2X123}');
 
		// Load the answers into an array if they are numbers
		var answers = new Array();
		loadArray(answer1, answer2, answer3);
		function loadArray() {
			$(arguments).each(function(i, ans){
				if(!isNaN(ans) && ans.length > 0) {
					answers.push(ans);
				}
			});
		}
 
		// Sum of the array items
		var total = 0;
		$(answers).each(function(i){
			total += Number(this);
		});
 
		// Average of the array items
		var average = total/answers.length;
 
		// Load the text question		
		$('input.text').val(average);
 
		// Submit the page
		document.limesurvey.submit();
	});
 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 5 years 2 months ago by tpartner.

Please Log in to join the conversation.

More
5 years 2 months ago #70903 by misterdot
Yes - works perfectly. Thanks so much!

Please Log in to join the conversation.

More
5 years 2 months ago #71049 by TMSWhite
Matt-

Here's how you would do it using version 1.92 with Expression Manager .

Say your questions are named (have code values) A-D, and they are all on page 1 Create a hidden Equation question type called Avg on page 2, and put this in its text field:
{if(count(A,B,C,D)==0,'N/A',sum(A,B,C,D)/count(A,B,C,D))}

This will store the average of the answered scores in the Avg field in your database, but the users won't see the computation. The count() function counts the number of non-empty responses; and the sum() function adds them. To avoid divide by zero, I first checked that at least one question was answered.

/Tom

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form