HTML Button: Redirect on click

79,245

Solution 1

Here's one way of doing it with your present code (submit-type button) using PHP's header() function.

(handler.php)

<?php
if(isset($_POST['submit'])){
header("Location: http://www.example.com/page.php");
exit;
}

And I'm assuming with the code you have in your question, resembling something to the affect of:

<form action="handler.php" method="post">
Username: 
<input type='text' name='username' />
<input type='submit' name='submit' value='Register' class='register' />
</form>

Of course I didn't include the possible $username=$_POST['username']; that could be in your PHP, depending on how you will be using it.


EDIT

Upon reading mplungjan's comment have made a slight name change. I've yet to know why using the name submit is considered unsafe, after trying to find the (or a) reason why on Google. I'm hoping to get or find an answer to this affect.

(Edit-findings) See further information below that I found to date.

(handler.php)

<?php
if(isset($_POST['reg_button'])){
header("Location: http://www.example.com/page.php");
exit;
}

And I'm assuming with the code you have in your question, resembling something to the affect of:

<form action="handler.php" method="post">
Username: 
<input type='text' name='username' />
<input type='submit' name='reg_button' value='Register' class='register' />
</form>

Findings:

Article(s) I've come across on the subject that mplungjan mentioned in comments:


If you're going to use a PHP (server-side) method, consider using the following, as borrowed from this website's article on Cross site scripting.

<input name="foo" value="<?php print htmlspecialchars($foo); ?>">

and in your case:

<input type='submit' name='reg_button' value='<?php print htmlspecialchars($reg_button); ?>' class='register' />

Borrowed from mplungjan's comment:

1) never call a submit button name="submit"
2) use a link or a button <input type="button" onclick="location='somepage.html'" />
3) Just use name="Submit" or submitted and problems will be avoided.

(Thanks for the extra input mplungjan).

Solution 2

You have to understand the default behavior of the tag you're using. The submit input tag, sends the user to the form action. I'm not sure what you're trying to do, so I'm not sure if you're even using the right tag. Perhaps consider anchor tags?

My best guess, given the vague question is:

<form action="{URL}" method="post">
  <input type='submit' name='submit' value='Register' class='register' />
</form>

or

<a href="{URL}">Register</a>

Solution 3

<input type="button" onclick="window.location='YourUrlHere'" class="register" value="Register"/>

Solution 4

You can use anchor tag for this problem, An anchor tag is used to redirect from one page to another page by just one click. Here you can use this as follow:

 <a href="url"><input type='submit' name='submit' value='Register' class='register' /></a>

Note: href is the tag which contains the path of your desired destination. That's it, Keep coding... :)

Share:
79,245
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years
    <input type='submit' name='submit' value='Register' class='register' />
    

    how do I make this link to a website on click?

  • mplungjan
    mplungjan over 10 years
    Many if not most browsers will make it impossible to submit a form using standard script if form.submit() is a button and not an event handler
  • mplungjan
    mplungjan over 10 years
    Almost exact copy of my comment
  • Funk Forty Niner
    Funk Forty Niner over 10 years
    But my answer doesn't consist of form.submit() (sounds like JS) or am I missing something? I'm still Googling this as we speak and have come up with a few reasons why, however, millions of pages out there exists in using a form variable to get the info passed from a form to a PHP handler. @mplungjan Relying on JS to get variable info, is not as reliable a method than server-side.
  • mplungjan
    mplungjan over 10 years
    But it will one day and then it will take forever to figure out. Just use name="Submit" or submitted and problems will be avoided. Anyway always double check on server
  • Funk Forty Niner
    Funk Forty Niner over 10 years
    I understand what you're saying, yet people in the future will still be able to disable JS, rendering a form useless, then having to scramble on using a different method, then what is already in place, and has been for years. I couldn't find anything yet on PHP.net to say otherwise, or use a different method to guard against potential name changing by malicious scripts etc. @mplungjan
  • mplungjan
    mplungjan over 10 years
    I use JS extensively to enhance my pages. If you disable JS you will get less functionality. Up to you.
  • Funk Forty Niner
    Funk Forty Niner over 10 years
    PHP can't rely, nor was it conceived to rely on JS for data flow/handling. Enhancing and functionality are two different animals altogether. @mplungjan
  • mplungjan
    mplungjan over 10 years
    Let's agree to disagree.
  • Funk Forty Niner
    Funk Forty Niner over 10 years
    (lol, ok) It's an AND/OR thing. However I didn't (completely) disagree with what you said, and is definitely noted. @mplungjan You did make a point.
  • PoloHoleSet
    PoloHoleSet over 5 years
    @mplungjan - I believe FunkFortyNiner is referring to the fact that, with all the auto-play advertisements, videos and pop-ups/overs that client-side technology gets used to assault end-users, many of THEM will block, disable or turn off JS. It's great that you use JS to enhance your pages, I do too, but then I run into customers who call in and say the pages don't work..... so I have to go to a php/server solution or demand that they enable JS when they don't want to.
  • mplungjan
    mplungjan over 5 years
    Try disabling JS these days on popular sites. All the React, Angular and Vue using API calls only will likely give you a blank screen