如何在用户点击链接时运行PHP代码? [英] How do I run PHP code when a user clicks on a link?
问题描述
我想让一个页面在用户点击一个链接时运行一些PHP代码,而不用重定向它们。这可能与
I want to have a page run some PHP code when a user clicks on a link, without redirecting them. Is this possible with
<a href=""></a>
或使用javascript onclick事件?
or with the javascript onclick event?
推荐答案
是的,你需要一个由onclick触发的javascript函数,它执行页面的AJAX加载,然后返回false,这样他们就不会在浏览器中重定向。您可以在jQuery中使用以下内容:
Yeah, you'd need to have a javascript function triggered by an onclick that does an AJAX load of a page and then returns false, that way they won't be redirected in the browser. You could use the following in jQuery, if that's acceptable for your project:
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function doSomething() {
$.get("somepage.php");
return false;
}
</script>
<a href="#" onclick="doSomething();">Click Me!</a>
如果您需要使用表单值(也可以使用$ .post ()方法)。
You could also do a post-back if you need to use form values (use the $.post() method).
这篇关于如何在用户点击链接时运行PHP代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!