Auto submit a webform on page load

I've previously written about using PHP to auto populate a form with URL query string values. In this post I'm going to describe how you can auto-submit that form so that the user doesn't even need to click the submit button.

This script will auto-submit the form once the page has loaded provided that 'autosubmit=1' is present in the URL's query string, eg. http://ww.example.com/testform?autosubmit=1

<?php if ($_REQUEST['autosubmit']) { ?>

    <script>
    (function ($) {
     
        $(document).ready(function(){
            $('form#theFormId').submit();
        });
     
    })(jQuery);
    </script>

<? } ?>

The above script uses a combination of PHP and jQuery, but it could be done using only jQuery - see this post about using jQuery to populate a form with URL values. Personally I prefer the PHP version as the jQuery code is only rendered if the 'autosubmit=1' value is present in the URL.

 

Justin Chevallier

Justin Chevallier

Avid Drupal site builder & user for +10 years.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.