Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Button: Redirect on click

<input type='submit' name='submit' value='Register' class='register' />

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

like image 536
LongSimple Avatar asked Jan 22 '26 09:01

LongSimple


2 Answers

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:

  • Why is the NAME attribute considered unsafe?
  • Cross site scripting
  • On php-security.org

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

like image 97
Funk Forty Niner Avatar answered Jan 23 '26 21:01

Funk Forty Niner


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>
like image 28
Jackson Leung Avatar answered Jan 23 '26 22:01

Jackson Leung