如何在PHP中使用onclick事件重定向页面? [英] How to redirect a page using onclick event in php?

查看:94
本文介绍了如何在PHP中使用onclick事件重定向页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了这段代码,但它不起作用,

 < input type =buttonvalue =Home class =homebuttonid =btnHomeonClick =<?php header(Location:/index.php);?> /> 


解决方案

不能使用客户端的php代码。您需要使用javascript。

 < input type =buttonvalue =Homeclass =homebuttonid = btnHome
onClick =document.location.href ='some / page'/>

然而,你真的不应该使用内联js(就像这里的onclick)。在此处研究: https://www.google。 com / search?q =为什么+ is + inline + js + bad%3F b
$ b 这是干净的做法: 现场演示(点击)。



标记:

 < button id =myBtn>重定向< / button> 

JavaScript:

  var btn = document.getElementById('myBtn'); 
btn.addEventListener('click',function(){
document.location.href ='some / page';
});

如果您需要使用php编写位置:

 < button id =myBtn>重定向< / button> 
< script>
var btn = document.getElementById('myBtn');
btn.addEventListener('click',function(){
document.location.href ='<?php echo $ page;?>';
});
< / script>


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"); ?>" />

解决方案

You can't use php code client-side. You need to use 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

Here's a clean way of doing this: Live demo (click).

Markup:

<button id="myBtn">Redirect</button>

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:

  <button id="myBtn">Redirect</button>
  <script>
    var btn = document.getElementById('myBtn');
    btn.addEventListener('click', function() {
      document.location.href = '<?php echo $page; ?>';
    });
  </script>

这篇关于如何在PHP中使用onclick事件重定向页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆