Welcome, Guest
Username: Password: Remember me

TOPIC: Intercept Save All action

Intercept Save All action 11 months 2 weeks ago #100649

Hi,

I would like to ask your help regarding an issue. I need to intercept the Save All action in a survey and perform an ajax call and then let Save All to continue.

Although I have succeeded this kind of functionality with the Next button I cannot make it work with Save All.

I run Version 1.92+(120919) [pls take under consideration that update to new version is not a possibility at the moment]

Thanks in advance!
The administrator has disabled public write access.

Intercept Save All action 11 months 2 weeks ago #100764

  • tpartner
  • tpartner's Avatar
  • NOW ONLINE
  • LimeSurvey Team
  • Posts: 4207
  • Thank you received: 791
  • Karma: 360
You should be able to do something like this. The script below clears the original save-all click event, adds an AJAX call and the re-fires the original save-all click event after the AJAX call is finished.
<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
 
		var oldClick = new Function($('input.saveall').attr('onClick'));
 
		// Clear the original save-all click event and add a new one
		$('input.saveall').attr('onclick', '').click(function(event) {
			$.ajax({
				url: 'http://path/to/your/file.php',
				async: true,
				data: {var1: 'some data'},
				success: function(results){
					// Do something with the returned data
					alert(results);
 
					// Fire the original save-all click event
					oldClick();
				},
				error: function(){
					// Oops, the AJAX call failed
					alert('Fail!');
				}
			});
		});
	});
</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.
The administrator has disabled public write access.

Intercept Save All action 11 months 2 weeks ago #100809

Tony,

thanks for the reply!

I put the script you provided both at question (level) and at group (level), separately each time, and still Save All works as before :)

Even if I try to change the onclick event of Save All to do just a simple alert, I cannot!

Any thoughts here?
The administrator has disabled public write access.

Intercept Save All action 11 months 2 weeks ago #100814

  • tpartner
  • tpartner's Avatar
  • NOW ONLINE
  • LimeSurvey Team
  • Posts: 4207
  • Thank you received: 791
  • Karma: 360
That's odd, I just tested and it worked as expected.

Do you have any JavaScript errors?

Here's a little working sample that interrupts the save-all function, counts down a timer for 5 seconds and then carries on with the save-all function.

File Attachment:

File Name: limesurvey_survey_774328.lss
File Size: 13 KB


The code is in the question text:
<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
 
		var oldClick = new Function($('input.saveall').attr('onClick'));
 
		var t = '';
 
		$('input.saveall').attr('onclick', '').click(function(event) {
			var timer = '<div class="timer" style="position:absolute; top: 100px; left: 50%; margin-left:-50px; width: 100px; height: 50px; background:black; text-align:center; line-height: 50px; color: white; font-size:200%;"></div>';
			$('body').append(timer);
 
			// Update timer
			function updateTime(remainSec){ 
 
			$('input.saveall').unbind('click');
 
				$('.timer').text(remainSec);
 
				if(remainSec != 0) {
					remainSec--;
 
					t = setTimeout(function() { 
						updateTime(remainSec);
					}, 1000);
				}
				else {
					// Timer is expired so carry on with the saveall
					oldClick();
				}
			}
 
			// Start the timer
			updateTime(5)
		});
	});
</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: 7 months 2 weeks ago by tpartner.
The administrator has disabled public write access.

Intercept Save All action 11 months 1 week ago #100867

Tony,

it works perfectly!!

The problem was that $('input.saveall') could not find anything.
So fixing that to $("input[name='saveallbtn']") it worked like a charm.

Thanx!!
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.112 seconds
Donation Image