HTML Button: Redirect on click

LongSimple picture LongSimple · Jan 19, 2014 · Viewed 77.9k times · Source
<input type='submit' name='submit' value='Register' class='register' />

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

Answer

Funk Forty Niner picture Funk Forty Niner · Jan 19, 2014

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).