How to send a form without using a button

Asked

Viewed 43 times

0

Good wanted when the user clicks off the input the form was sent, and also that the page does not reload

my php index.

<!DOCTYPE html>
<html>
<head>
    <title>Exemplo</title>
    <meta charset="utf-8">
</head>
<body>
    <div id="formulario">
        <button onclick="carregaFormulario()">Gera formulario</button>
    </div>

    <script type="text/javascript">
        function carregaFormulario() {
            let divConteudo = document.getElementById('formulario')
            let formulario = document.createElement('form')
            let input = document.createElement('input')

            input.name = 'nome'
            formulario.appendChild(input)
            formulario.method = 'post'
            formulario.action = '#'
            input.onblur = () =>{
                //submeter o formulario
                console.log('Estou aqui')
            }

            divConteudo.appendChild(formulario)
        }

    </script>

</body>
</html> 
  • Hello Thor, for the part you wish the page does not reload is worth deepening your knowledge with Ajax or some other framework that implements it.

1 answer

0

You can use Ajax to submit the form without reloading the page.


    $.ajax({
      "type":"post",
      "url":"suaurlaqui",
      "data": "os dados no formato JSON",
      "success": function(data){
           //Implementa alguma coisa se sucesso
      },
      "error": function (erro){
           //Implementa alguma coisa se erro
       }
    })

In this link you have full access to the Ajax API in jQuery. http://api.jquery.com/jquery.ajax/

Browser other questions tagged

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