2013-06-03 07:22 阅读 32

PHP&js / jquery:点击[关闭]如何运行PHP代码

How can I run PHP code on click of a link? (<a>link here</a>)?

I know how listen for a click in JavaScript or JQuery And then perform JavaScript or JQuery, but how would I do PHP?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dq13733519262 dq13733519262 2013-06-03 07:27

    PHP code is a server side code while JavaScript is a client side. Thus PHP code executes on the server while JS code executes in client's browser. There are no way to execute PHP code in client's browser but what you've probably needs is AJAX.

    AJAX requests can be called from client's browser to run some custom server side code which will return the result to browser. The way to do it from jQuery is using $.ajax() method or one of it variants - $.post() and $.get().

    点赞 评论 复制链接分享
  • douchen2595 douchen2595 2013-06-03 07:33

    In case you don't plan to use any ajax request, You may put your php code in a page and onclick on the hyperlink, you change the src of a frame to be the php page that you created. note that the iframe has a width and height of 0 so it doesn't appear in the browser.

    <iframe src='' id="myframe" frameborder="0" height="0" width="0"></iframe>
    <a href='javascript:document.getElementById("myframe").src="hello.php"'>link here</a>
    点赞 评论 复制链接分享