Send form without knowing the name

0

I have a form that changes its name constantly, and I want to give the submit in it by a function. In this function "changes" it gets the name of the form, but this way I did it does not work. The "+ ide +" of the form is a random number, so move it to the "change" function. If someone can help me and explain how to make it work what I want to do.

<form method='get' action='GG.php' name='alteracao"+ide+"'>
    <button onclick='altera(alteracao"+ide+")'>Enviar</button>
</form>

function altera(ide){   
        document.ide.submit();
    }
    
asked by anonymous 25.07.2018 / 20:00

3 answers

1

Instead of generating a name random for the form, generate a id random:

function altera(ide) {
    var form = document.getElementById(ide);
    alert(form.teste.value); // Para você se certificar que pegou o objeto correto.
    form.submit();
}
<form method='get' action='GG.php' id='alteracao123'>
    <input name="teste" type="hidden" value="Segredo secreto!" />
    <button onclick='altera("alteracao123")'>Enviar</button>
</form>

In the example above, I'm assuming 123 is the automatically generated number.

    
25.07.2018 / 20:44
1

You can get the name of the form by the relation between the button and itself

function altera(meuForm){
  alert(meuForm.name);
  meuForm.submit();
}
<form method='get' action='GG.php' name="esse form tem um nome muito difícil"  id='alteracao"+ide+"'>
    <button type="button" onclick='altera(this.parentNode)'>Enviar</button>
</form>
    
25.07.2018 / 21:28
0

You can give your form an 'alter' id

<form method='get' action='GG.php' name='alteracao"+ide+"' id="altera">
    <button onclick='altera(alteracao"+ide+")>Enviar</button>
</form>

With this it is possible to check when to submit 'submit' in form and run its function:

document.getElementById('altera').addEventListener('submit', function(
    // Aqui você realiza suas funções
    altera();
));
    
25.07.2018 / 20:10