direct page with onclick inside a form

Asked

Viewed 2,915 times

0

Basically what I need is to put a 'back' button inside my form. It has to be inside for aesthetic reasons, the back button would be inline with the send button.

I made a short code:

    <form action="php/***.php" method="post">
        <p>DIGITE SEU NOME:
        <input type="text"  placeholder="nome" name="nome" id="nome"><p>

        <input type="submit">
        <a onClick="window.location='home.php';">
            <button style="margin-left: 10%" class="bt2" >Voltar</button>
        </a>
    </form>

But when I click the 'back' button it directs to the action page in the form, not to the onclick home page.

I managed to make it work on one page, but if I copy exactly the same code, it doesn’t work on others. Very strange.

  • Instead of using onClick, wouldn’t it be better to use href? <a href="home.php">Voltar</a>

  • using href there the code will not leave even, because the action of the form overlaps. As I had said, on one page I was able to make js override html. But when I played the code it no longer worked =/

  • Can’t take out the "a" and put onClick right on the button?

1 answer

1


Very strange to use a button inside a <a>, because a button by itself already has the possibility to make a redirect. I would recommend instead of doing this, put in the button one onclick="location.href='home.php'" and remove that <a>, which would be the most appropriate.

But the real problem is that there’s one missing type="button" on the button, because the button is by default a submit, thus without the type="button", it will call the URL of action form.

Do the following: remove this <a> and leave the button like this, with type and onclick:

<button type="button" onclick="location.href='home.php'" style="margin-left: 10%" class="bt2" >Voltar</button>

Browser other questions tagged

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