当用户单击链接时如何运行 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,这样他们就赢了t 在浏览器中被重定向.如果您的项目可以接受,您可以在 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屋!