robert0
robert0

Reputation: 445

Executing form autosubmit on page load?

I have tried many of the scripts and jquery examples here on this website, but none of them worked for my specific situation.

Basically I'm trying to autosubmit a form (without user needing to press submit).

As soon as the page loads, the autosubmit will be triggered.

My Form:

   <form method="post" id="adduser" action="<?php the_permalink(); ?>" >                    
   <input type="text" name="confirmed" id="confirmed" value="yes" class="regular-text" />
   <p class="form-submit">
   <?php echo $referer; ?>
   <input name="updateuser" type="submit" id="updateuser" class="submit button" value="<?php _e('Update', 'profile'); ?>" />
   <?php wp_nonce_field( 'update-user' ) ?>
   <input name="action" type="hidden" id="action" value="update-user" />
   </p>
   </form>

As you can see I have php in the form's action, so javascript/jquery that needs to set an action URL inside the script won't work, as they don't allow a php.

Any help would be greatly appreciated.

Upvotes: 0

Views: 102

Answers (3)

isaacsan 123
isaacsan 123

Reputation: 1158

You could set an event to trigger after the document has loaded, like this:

<script>
    document.addEventListener('DOMContentLoaded', (e) => {
        const form = document.querySelector('#adduser')

        form.submit()
    })
</script>

This will submit the form right after all the contents in the DOM will be loaded.

Upvotes: 3

Le Na
Le Na

Reputation: 84

try the following code:

<script>
    $(document).ready(function () {
        var formData = $('#adduser').serializeArray();
        if(formData.length != 0) {
            $("#adduser").submit();
        }
    })
</script>

Upvotes: 0

ferikeem
ferikeem

Reputation: 537

$(function () {
  $('#adduser').submit();
}

This should work.

Upvotes: 0

Related Questions