php 如何在php中使用onclick事件重定向页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20769606/
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
How to redirect a page using onclick event in php?
提问by Harshana
I tried with this code but it's not working,
我试过这段代码,但它不起作用,
<input type="button" value="Home" class="homebutton" id="btnHome" onClick="<?php header("Location: /index.php"); ?>" />
回答by m59
You can't use php code client-side. You need to use javascript.
您不能在客户端使用 php 代码。你需要使用javascript。
<input type="button" value="Home" class="homebutton" id="btnHome"
onClick="document.location.href='some/page'" />
However, you really shouldn't be using inline js (like onclick here). Study about this here: https://www.google.com/search?q=Why+is+inline+js+bad%3F
但是,您真的不应该使用内联 js(例如这里的 onclick)。在这里研究一下:https: //www.google.com/search?q=Why+is+inline+js+bad%3F
Here's a clean way of doing this: Live demo (click).
这是一个干净的方法:现场演示(单击)。
Markup:
标记:
<button id="myBtn">Redirect</button>
JavaScript:
JavaScript:
var btn = document.getElementById('myBtn');
btn.addEventListener('click', function() {
document.location.href = 'some/page';
});
If you need to write in the location with php:
如果需要在位置用php写:
<button id="myBtn">Redirect</button>
<script>
var btn = document.getElementById('myBtn');
btn.addEventListener('click', function() {
document.location.href = '<?php echo $page; ?>';
});
</script>
回答by Null
you are using onclickwhich is javascript event.
there is two ways
您正在使用onclick哪个是 javascript 事件。有两种方法
Javascript
Javascript
<input type="button" value="Home" class="homebutton" id="btnHome"
onClick="window.location = 'http://google.com'" />
Or PHP
或 PHP
create another page as redirect.php and put
创建另一个页面作为redirect.php并放置
<?php header('location : google.com') ?>
and insert this link on any page within the same directory
并在同一目录中的任何页面上插入此链接
<a href="redirect.php">google<a/>
hope this helps its simplest!!
希望这有助于它的最简单!
回答by Jakub
Why do people keep confusing php and javascript?
为什么人们总是混淆 php 和 javascript?
PHP is SERVER SIDE
JAVASCRIPT (like onclick) is CLIENT SIDE
PHP 是服务器端
JAVASCRIPT(如 onclick)是客户端
You will have to use justjavascript to redirect. Otherwise if you want PHP involved you can use an AJAX call to log a hit or whatever you like to send back a URL or additional detail.
您将不得不仅使用javascript 进行重定向。否则,如果您希望涉及 PHP,您可以使用 AJAX 调用来记录命中或任何您喜欢发送回 URL 或其他详细信息的内容。
回答by Sudheesh.R
Please try this
请试试这个
<input type="button" value="Home" class="homebutton" id="btnHome" onClick="Javascript:window.location.href = 'http://www.website.com/index.php';" />
window.location.href example:
window.location.href 示例:
window.location.href = 'http://www.google.com'; //Will take you to Google.
window.open() example:
window.open() 示例:
window.open('http://www.google.com'); //This will open Google in a new window.

