php HTML 按钮:单击时重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21222227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:55:28  来源:igfitidea点击:

HTML Button: Redirect on click

phphtmlloginforummybb

提问by LongSimple

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

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

我如何通过点击链接到一个网站?

回答by Funk Forty Niner

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

这是使用 PHPheader()函数使用当前代码(提交类型按钮)执行此操作的一种方法。

(handler.php)

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

当然,我没有包括$username=$_POST['username'];可能存在于您的 PHP 中的可能性,这取决于您将如何使用它。



EDIT

编辑

Upon reading mplungjan's commenthave made a slight name change. I've yet to know why using the name submitis 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.

经阅读mplungjan's comment已做了轻微的名称更改。submit在尝试在 Google 上找到(或一个)原因后,我还不知道为什么使用该名称被认为是不安全的。我希望得到或找到这种影响的答案。

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

(编辑结果) 请参阅以下我迄今为止发现的更多信息。

(handler.php)

(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 mplungjanmentioned in comments:

mplungjan在评论中提到的主题上遇到的文章:



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.

如果您打算使用 PHP(服务器端)方法,请考虑使用以下内容,如从本网站关于跨站点脚本的文章中借用的。

<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'scomment:

借用mplungjan's评论

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 submittedand problems will be avoided.

1) 从不调用提交按钮name="submit"
2) 使用链接或按钮<input type="button" onclick="location='somepage.html'" />
3) 只使用name="Submit"submitted,问题将被避免。

(Thanks for the extra input mplungjan).

(感谢您的额外输入mplungjan)。

回答by Mark

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

回答by Hymanson Leung

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>

回答by Amrinder Singh

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

注意:href 是包含所需目的地路径的标签。就是这样,继续编码... :)

回答by Stephane Paquet

You can go for a <a href="{URL}">Register</a>as just said or if you want you can also use the button tag <button onclick="myFunction()">Click me</button>where myFunctionis your JavaScript code to an other page

您可以<a href="{URL}">Register</a>按照刚才所说的那样使用,或者如果您愿意,也可以使用按钮标记<button onclick="myFunction()">Click me</button>,其中myFunction是您到其他页面的 JavaScript 代码