当用户单击链接时如何运行 PHP 代码?
我想让页面在用户单击链接时运行一些 PHP 代码,而不重定向它们。 这可以通过
<a href=""></a>
javascript onclick 事件实现吗?
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>
or with the javascript onclick event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我知道这篇文章很旧,但我只是想添加我的答案!
您说要在不指示的情况下注销用户...此方法确实会重定向,但会将用户返回到他们所在的页面! 这是我的实现:
我们开始吧!
从完整 url 获取文件名的代码不能防止错误。 例如,如果查询字符串中包含未转义的“/”,则会失败。
然而,有很多脚本可以从 url 获取文件名!
快乐编码!
亚历克斯
I know this post is old but I just wanted to add my answer!
You said to log a user out WITHOUT directing... this method DOES redirect but it returns the user to the page they were on! here's my implementation:
and there we go!
the code that gets the file name from the full url isn't bug proof. for example if query strings are involved with un-escaped '/' in them, it will fail.
However there are many scripts out there to get the filename from url!
Happy Coding!
Alex
是的,您需要有一个由 onclick 触发的 javascript 函数,该函数对页面进行 AJAX 加载,然后返回 false,这样它们就不会在浏览器中重定向。 如果您的项目可以接受,您可以在 jQuery 中使用以下内容:
如果您需要使用表单值(使用 $.post() 方法),您也可以进行回发。
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:
You could also do a post-back if you need to use form values (use the $.post() method).
正如其他人所建议的,使用 JavaScript 进行 AJAX 调用。
http://docs.jquery.com/Ajax/jQuery.ajax
As others have suggested, use JavaScript to make an AJAX call.
http://docs.jquery.com/Ajax/jQuery.ajax
如果你还没有安装jquery(因为你只是初学者什么的),请使用这段代码:
If you haven't yet installed jquery (because you're just a beginner or something), use this bit of code:
当用户单击链接而不离开页面时,除非使用 AJAX,否则无法运行 PHP。 PHP 是一种服务器端脚本语言,这意味着浏览器看到页面的那一刻,其中就没有 PHP 了。
与Javascript不同的是,PHP完全运行在服务器上,浏览器如果跟在后面,就不知道如何解释它。 调用 PHP 代码的唯一方法是通过刷新页面或使用 javascript 获取页面来发出页面请求。
在 AJAX 解决方案中,页面基本上使用 javascript 将页面请求发送到域中的另一个页面。 然后,Javascript 会获取您决定在响应中
echo
的任何内容,并且它可以解析它并从那里执行它想要的操作。 创建响应时,您还可以执行任何后端操作,例如更新数据库。You cant run PHP when a user clicks on a link without leaving the page unless you use AJAX. PHP is a serverside scripting language, meaning the second that the browser sees the page, there is no PHP in it.
Unlike Javascript, PHP is ran completely on the server, and browser wouldn't know how to interpret it if it bit them on the rear. The only way to invoke PHP code is to make a Page request, by either refreshing the page, or using javascript to go fetch a page.
In an AJAX Solution, basically the page uses javascript to send a page request to another page on your domain. Javascript then gets whatever you decide to
echo
in the response, and it can parse it and do what it wants from there. When you are creating the response, you can also do any backend stuff like updating databases.唯一更好的方法是 AJAX,每个人都在他们的帖子中建议。
另一种方法是使用 IFrame,如下所示:
现在您将在 IFrame 中获得输出(您可以将 IFrame 放置在页面中任何需要的位置,或者事件隐藏它以及脚本的结果)。
希望非 Ajax 解决方案更好。
There is the only better way is AJAX as everyone is suggest in their posts.
The alternative is using IFrames like below:
Now you will get the output in IFrame (you can place IFrame wherever you need in the page or event hide it and the result from the script).
Hope for non Ajax solution this is better.
好吧,你说没有重定向。 嗯,它是一个 javascript 代码:
尝试一下:)希望你喜欢它
Well you said without redirecting. Well its a javascript code:
give it a try :) hope you like it
要么将用户发送到另一个执行此操作的页面
,要么使用 ajax 执行此操作
either send the user to another page which does it
or do it with ajax
这应该也有效,
谢谢,
cowtipper
This should work as well
Thanks,
cowtipper