Welcome, Guest
Username: Password: Remember me

TOPIC: Automatically submit with blank answers

Automatically submit with blank answers 2 years 7 months ago #70881

  • misterdot
  • misterdot's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 35
  • Thank you received: 1
  • Karma: 0
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
The administrator has disabled public write access.

Re: Automatically submit with blank answers 2 years 7 months ago #70891

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4050
  • Thank you received: 740
  • Karma: 341
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Automatically submit with blank answers 2 years 7 months ago #70896

  • misterdot
  • misterdot's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 35
  • Thank you received: 1
  • Karma: 0
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.
The administrator has disabled public write access.

Re: Automatically submit with blank answers 2 years 7 months ago #70902

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4050
  • Thank you received: 740
  • Karma: 341
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 2 years 7 months ago by tpartner.
The administrator has disabled public write access.

Re: Automatically submit with blank answers 2 years 7 months ago #70903

  • misterdot
  • misterdot's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 35
  • Thank you received: 1
  • Karma: 0
Yes - works perfectly. Thanks so much!
The administrator has disabled public write access.

Re: Automatically submit with blank answers 2 years 7 months ago #71049

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
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
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.223 seconds
Donation Image