How to include an action in the HTML5 sign-up button?

Asked

Viewed 19,877 times

4

I have the following code:

<form action="paginas/login.php">
    <p id="log">Usuário: </p> <input id="cmpG" type="text" name="usuario"/>
    <p id="log">Senha: </p> <input id="cmpG" type="password" name="senha" size=8 maxlength=8/>
    <p id="log">A senha deve conter 08 caracteres (alfanumerico)</p>
    <!-- Barra de botões -->
    <div id="barra">
        <input id="bnt" type="submit" value="Login"/>
        <input id="bnt" type="reset" value="Limpar"/>
        <BUTTON >Cadastrar</BUTTON>
    </div>
</form>

Well, I don’t know how to do the sign-up button, I tried with input and kind submit, but it goes to the page login.php also, how to make it go to another page?

  • 1

    And you expected him to do what? It doesn’t go to another page magically. Or it has to be in another form or you have to create a custom action for it.

  • Went to a page cadastro.php but I don’t know if that’s possible, I’m just trying to do rs, without reference or anything.. The idea would be to have two pages as destination for the form, if the user presses the X button he goes to page X and if press Y go to page Y, both passing parameters.

2 answers

6


You have two options:

  • change the action with Javascript and make Submit via Javascript
  • change the action with the formaction added in HTML5 (IE9+)

Either way you can have two Submit buttons:

<div id="barra">
    <input class="bnt" name="" type="submit" value="login"/>
    <input class="bnt" type="submit" value="cadastrar"/>
    <input class="bnt" type="reset" value="Limpar"/>
</div>

Notice I’ve changed too id for class because Ids have to be unique.

using Javascript:

var form = document.querySelector('form');
var inputDiv = document.getElementById('barra');
var urls = {
    cadastrar: 'paginas/cadastro.php',
    login: 'paginas/login.php'
};
function verificarDestino(e){
    e.preventDefault();
    console.log(e.target);
    var tipo = e.target.value;
    var url = urls[tipo];
    form.action = url;
    alert(url); // só para verificar
    form.submit();
}
inputDiv.addEventListener('click', verificarDestino);

jsFiddle: http://jsfiddle.net/4zveq0y4/

using HTML5

<div id="barra">
    <input class="bnt" type="submit" value ="Login" formaction="paginas/login.php"/>
    <input class="bnt" type="submit" value ="Cadastrar" formaction="paginas/cadastro.php"/>
    <input class="bnt" type="reset" value="Limpar"/>
</div>

jsFiddle: http://jsfiddle.net/4zveq0y4/2/

This way the input element itself re-writes the action form.

Note:

In my answer I use input but if you want to use button the logic is the same and the formaction is used the same way too.

4

Or you have to do:

<form action="paginas/login.php">
    <p id="log">Usuário: </p> <input id="cmpG" type="text" name="usuario"/>
    <p id="log">Senha: </p> <input id="cmpG" type="password" name="senha" size=8 maxlength=8/>
    <p id="log">A senha deve conter 08 caracteres (alfanumerico)</p>
    <!-- Barra de botões -->
    <div id="barra">
        <input id="bnt" type="submit" value="Login"/></a>
        <input id="bnt" type="reset" value="Limpar"/>
    </div>
</form>
<form action="paginas/cadastrar.php">
    <BUTTON >Cadastrar</BUTTON>
</form>

Or use an event onclick to give him a personalized action. Taking advantage of his own code would be something like this:

<BUTTON onclick="window.location.href='paginas/cadastrar.php'">Cadastrar</BUTTON>

I put in the Github for future reference.

Maybe you want to do it another way but I’ve given you a simplified example.

  • If you have 2 Forms it will not send the data of the other, in the second case sends the empty form.

  • @Sergio yes, of course, but what he wants is to call a registration page that is not the same form. In the background is even the most correct. Having a button inside a form but not using his data is semantically wrong, although it works. And my understanding is this, if he wants to send the data, in fact the sending solution based on the click is the appropriate one, so I put both. Your answer, although correct and more complete, does not use the <button> which is what he wants to wear.

  • 1

    Okay, I understand your logic better. In that case he could simply have an anchor for the sign-up page. I added to my reply info on button which is equal, well seen, thank you.

  • I agree, but I used what he wanted.

Browser other questions tagged

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