193

I have been trying the following:

<form action="/home" class="inline">
    <button class="float-left submit-button" >Home</button>
</form>

It seems to work but it goes to the page "/home?"

Is there a better way for me to make a button inside a form make the page go to a new location?

mattytommo
  • 54,375
  • 15
  • 123
  • 144
  • A form element can only take you to other pages within the same site. If you want to redirect to other websites, you need to use JavaScript's location.href or similar. – Adrian Wiik Dec 20 '19 at 13:43

5 Answers5

416

Just add an onclick event to the button:

<button onclick="location.href = 'www.yoursite.com';" id="myButton" class="float-left submit-button" >Home</button>

But you shouldn't really have it inline like that, instead, put it in a JS block and give the button an ID:

<button id="myButton" class="float-left submit-button" >Home</button>

<script type="text/javascript">
    document.getElementById("myButton").onclick = function () {
        location.href = "www.yoursite.com";
    };
</script>
Community
  • 1
  • 1
mattytommo
  • 54,375
  • 15
  • 123
  • 144
59

try

<button onclick="window.location.href='b.php'">Click me</button>
Ehmad Imtiaz
  • 626
  • 5
  • 6
18

Use this:

 <button onclick="window.location='page_name.php';" value="click here" />

Basically you are using javascript snippet to redirect and onclick event of the button to trigger it.

Atharva Kadlag
  • 302
  • 3
  • 13
Veer Shrivastav
  • 5,314
  • 11
  • 50
  • 83
11

Just another variation:

    <body>
    <button name="redirect" onClick="redirect()">

    <script type="text/javascript">
    function redirect()
    {
    var url = "http://www.(url).com";
    window.location(url);
    }
    </script>
Ben Taylor
  • 165
  • 1
  • 5
4

you could do so:

<button onclick="location.href='page'">

you could change the action attribute of the form on click the button:

<button class="float-left submit-button" onclick='myFun()'>Home</button>

<script>
myFun(){
$('form').attr('action','new path');
}
</script>
Muhammad Bekette
  • 1,336
  • 1
  • 23
  • 59