How to make a form submission without form?

2

The question may seem strange. But on a particular page I would like to make a submit , like a form, when clicking a button, but without the presence of the form in HTML.

Is there any way to simulate submitting a form, without it being on the page?

Example:

<!-- Faz um paranauê para submeter a página via post sem formulário -->
<button id="submit"></button>

Note : I do not need a form fill, or anything. I just need that, when the button is clicked, a form submission is sent (POST method). And it's not an ajax request.

    
asked by anonymous 07.12.2015 / 13:56

2 answers

6

If you just need to send the form, you could use ajax, but it does not seem to be the case, there is still a new html5 attribute that can help you, is form="..." and create a hidden form you will need javascript):

<!--// formulário oculto -->
<form action="rota" method="POST" id="meuformulario">
  <input type="hidden" name="chave1" value="1">
  <input type="hidden" name="chave2" value="2">
  <input type="hidden" name="chave3" value="3">
</form>

...

<p>....</p>


...    

<button type="submit" form="meuformulario" value="Chamar">Chamar (trigger)</button>

Source: link

In addition it is still possible to control the form with other attributes by <button> :

  • formaction change the form action pointed to by the form="" attribute

  • formenctype this attribute change the ecntype of the form pointed to by the form="" attribute, values that can be used:

    • application/x-www-form-urlencoded
    • multipart/form-data
    • text/plain
  • formmethod exchange method of <form> pointed to by attribute form=""

07.12.2015 / 14:15
2

Yes, there is a way. You can do the same way you use to submit a form via javascript, however you will only use what is created by document.createElement

So:

var fake_form = document.createElement('form');

fake_form.method = 'POST';

document.querySelector('#submit').addEventListener('click', function()
{
     fake_form.submit();
});
    
07.12.2015 / 13:56