Home > Software engineering >  Submit form one time after page load via Jquery
Submit form one time after page load via Jquery

Time:09-24

How to submit the cart request-quote form after page load? Below code is worked fine -

<?php if(!isset($_GET['updated'])) { ?>
<script>
jQuery(document).ready(function($) {
jQuery("#yith-ywraq-form").submit();
});
</script>
<?php } ?>

But avoid submitting forms, again and again, I reload to this action site.com/request-quote/?updated=data

But if someone refreshes the same page site.com/request-quote/?updated=data then I have no option to submit the form again on page load.

do you have a solution for this problem?

My requirement is I want to submit the cart request-quote form 1 time after page load?

CodePudding user response:

You may use localStorage for this. Take a flag in set some initial value of it. When the form submits update it's value. By checking the same you can avoid getting submit.

<?php if(!isset($_GET['updated'])) { ?>
    <script>
        if(!localStorage.getItem('isFormAlreadySubmit')){
            jQuery(document).ready(function($) {
                jQuery("#yith-ywraq-form").submit();
                localStorage.setItem('isFormAlreadySubmit', true);
            });
        }
    </script>
<?php } ?>

CodePudding user response:

You can move $_GET['updated'] to localStorage or sessionStorage, then you don't need to reload the page to ?updated=data page anymore.

  • localStorage will save updated value even when the browser is closed and reopened.
  • sessionStorage will save updated value but it will be cleared if the browser is closed.

That depends on you to choose which one. So I make a demo with sessionStorage which I prefer because when the user closes the browser, it will clear the updated value, maybe the next user in that device is another (public device). I also remove the check if(!isset($_GET['updated'])) condition.

<script>
  jQuery(document).ready(function($) {
    if (!sessionStorage.getItem('updated')) {
      jQuery("#yith-ywraq-form").submit();
      sessionStorage.setItem('updated', true);
    }
  });
</script>

Update: add live demo https://codepen.io/tuandaodev/pen/jOwvrRg?editors=1111

CodePudding user response:

You need to use a server side session variable to check (duplicate) Answer

  • Related