Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Intercept Save All action

  • dimkaraiskos
  • dimkaraiskos's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 5 months ago #100649 by dimkaraiskos
Intercept Save All action was created by dimkaraiskos
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 topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 5 months ago #100764 by tpartner
Replied by tpartner on topic Intercept Save All action
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.
Code:
<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.
The topic has been locked.
  • dimkaraiskos
  • dimkaraiskos's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 5 months ago #100809 by dimkaraiskos
Replied by dimkaraiskos on topic Intercept Save All action
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 topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 5 months ago - 10 years 1 month ago #100814 by tpartner
Replied by tpartner on topic Intercept Save All action
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...4328.lss
File Size:13 KB


The code is in the question text:
Code:
<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.
Last edit: 10 years 1 month ago by tpartner.
The topic has been locked.
  • dimkaraiskos
  • dimkaraiskos's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 5 months ago #100867 by dimkaraiskos
Replied by dimkaraiskos on topic Intercept Save All action
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 topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose