Send form without knowing the name

Asked

Viewed 44 times

-1

I have a form that changes its name constantly , and I want to give it Ubmit for a function. In this function "alters" it takes the name of the form, but this way I did it does not work. The "+ide+" form is a random number, so I pass it to the "alter" function. If anyone 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();
    }
  • There’s a ' missing in onclick. Other than that, I don’t understand what you’re trying to do. Where does the name of the form that always changes come from? Are you trying to figure out what the name is or trying to invent a new random name?

  • This ' I forgot when I wrote here. This "+ide+" it always changes, for example, it can be "alteracao10" or "alteracao55". But my doubt remains being in the function itself, it works by putting Document." ide". Submit(); where this "ide" is the parameter that mimes function receives?

  • But with you does the interaction on this "+ide+", is already coming from somewhere?

  • How are you generating the Formulars ?

  • This "+ide+" is generated by the database with an Autoincrement, @Leandrade

  • The +ide+ is replaced by a number only on the tag form or it’s also on the tag button?

  • The forms are generated by a createelement + innerHTML. That createelement creates the place where the form will appear and innerHTML places the form already pre-created in javacript. @Marcosjunior

  • @Victorstafusa on the button tag the +ide" is only used to send the parameter in the function, thus sending the exact name of the Form.

  • In that case would not only take the value of the name? Because the name no longer goes with the value of the bank?

Show 4 more comments

3 answers

1

Instead of generating one name random to 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 above example, I am assuming that 123 is the automatically generated number.

1

You can get the form name by the relationship between the button and the form 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>

  • Opa Thanks , this way worked here.

  • Consider the correct answer and give up ;)

0

You can give an id to your form as 'alters'

<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 give 'Submit' in the form and run its function:

document.getElementById('altera').addEventListener('submit', function(
    // Aqui você realiza suas funções
    altera();
));
  • There is a problem, I will have several forms that are generated by javascript when I click on a button, so I can have several forms without knowing what their Id, which falls in the same doubt that I have.

  • Try to set a default class for them, and put that same Elektor with that it will pick up the Submit from the form that was sent.

  • But the Submit event will only happen by the function I did, I’m using the function to "force" Submit

  • In case it is possible to rephrase your question, I didn’t quite understand what you want to know to submit to your function, it would just be that ide?

Browser other questions tagged

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