-1

So a little hard to explain in the title. Technically the script is being executed in order but does not wait for the next function to be done before executing the next. Is there any way to do this?

    <form action="" method="POST">
         <input type="submit" style="background:transparent;border:0;color:#ffffff;" name="approve" id="approve" value="approve shipment" />
         <input type="hidden" name="ShipmentDigest" value="<?php echo $responseArray['ShipmentConfirmResponse']['ShipmentDigest']['VALUE']; ?>" />
    </form>

Javascript:

<script>
     document.getElementById('approve').click();
     setTimeout(function(){
         window.print();
     },2000);
     close();
</script>

So basically I am generating an UPS label and upon if ($approve == 'approve shipment') then it will generate the label and download to server as well as I want it to pop up for the user to print and then regardless of selection of "Print" or "Cancel" close out that pop up window.

The issue is that the button is a form submit which then reloads the page and script just keeps executing if I remove the close(); Right now, the script will click on the approve button and then starts the timer and then closes before the time has completed. If I change the code to having close(); within the wait, it just keeps clicking on approve and never lets the timer run:

<script>
     document.getElementById('approve').click();
     setTimeout(function(){
         window.print();
         close();
     },2000);
</script>
Marz_89
  • 51
  • 2
  • 12
  • Take a look at this other question: https://stackoverflow.com/questions/6460630/close-window-automatically-after-printing-dialog-closes – gaheinrichs Mar 22 '18 at 23:07
  • Use AJAX, so your print happens after your Server response. Make sure you `formElement.addEventListener('submit', function(eventObj){ eventObj.preventDefault();})` or `formElement.onsubmit = function(){ return false; }`; – StackSlave Mar 22 '18 at 23:10
  • https://stackoverflow.com/questions/18325025/how-to-detect-window-print-finish – Ctznkane525 Mar 22 '18 at 23:12

1 Answers1

1

If the problem is that submitting your form is reloading your page, and you either don't want to reload the page at all, or would rather delay reloading it, then you might want to consider doing your POST via an XMLHttpRequest (https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest) rather than using a basic HTML form.

kshetline
  • 12,547
  • 4
  • 37
  • 73