Submit form when loading page

0

I have a form and I wanted the form to be loaded when the page was loaded. I'm using the code below, however it gets 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 infinitely. I thought I would run only once.

    
asked by anonymous 21.03.2017 / 04:14

1 answer

0

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

Enjoying this way of writing seems cleaner and simpler to me.

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