HTML Button: Redirect on click
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... :)
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on July 09, 2022Comments
-
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 over 10 yearsMany 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 over 10 yearsAlmost exact copy of my comment
-
Funk Forty Niner over 10 yearsBut 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 over 10 yearsBut 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 over 10 yearsI 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 over 10 yearsI use JS extensively to enhance my pages. If you disable JS you will get less functionality. Up to you.
-
Funk Forty Niner over 10 yearsPHP 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 over 10 yearsLet's agree to disagree.
-
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 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 over 5 yearsTry disabling JS these days on popular sites. All the React, Angular and Vue using API calls only will likely give you a blank screen