Submit form when loading the page

Asked

Viewed 794 times

0

I have a form and wanted that when being loaded the page, that form was submitted. I’m using the code below, but it’s in an infinite loop.

<form name="teste" action="inicial" method="post">
Nome do Usuário: <input type="text" name="user">
<input type="submit" value="Enviar">
</form>

JS

$(document).ready(function () {
    window.onload = function() {    
    document.forms[0].submit();
    }
});

When I load the page, it submits endlessly. I thought I’d only execute once.

  • tries to take out the window.onload = Function() { } and take the form with a $('form' selector). Ubmit();

  • If the request is going to the page itself, it will give an infinite loop, because it will submit to itself, and when it loads it will do it again. so eternally.

1 answer

0


It may be that your form is redirecting after being sent to the same link where it is, then it will submit again.

Taking advantage of this way of writing seems cleaner and simpler.

$(function(){ //Código escrito de outra forma
  $("[name='teste']").submit();
})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.