dongsi4547 2013-06-25 15:09
浏览 90
已采纳

通过HTML链接调用PHP函数(无表格)

I have a PHP Function that I would like to integrate into my (existing) web page. Further, I would like it to execute when the user clicks a link on the page. The function needs to accept the text of the link as an input argument.

Everything I've researched for sending data to a PHP script seems to involve using forms to obtain user input. The page needs to accept no user input, just send the link-text to the function and execute that function.

So I guess the question is two-part. First, how to execute a PHP script on link click. And second, how to pass page information to this function without the use of forms. I am open to the use of other technologies such as AJAX or JavaScript if necessary.

EDIT:: Specifically what I am trying to do. I have an HTML output representing documentation of some source code. On this output is a series of links (referring to code constructs in the source code) that, upon being clicked, will call some python function installed on the web server (which leads me to think it needs called via PHP). The python function, however, needs the name present on the link as an input argument.

Is there some sort of interaction I could achieve by having JavaScript gather the input and call the PHP function?

Sorry for the vagueness, I am INCREDIBLY new to web development. If anything is unclear let me know.

  • 写回答

7条回答 默认 最新

  • drdawt9210 2013-06-25 15:13
    关注

    You will need to use AJAX to accomplish this without leaving the page. Here is an example using jQuery and AJAX (this assumes you have already included the jQuery library):

    First File:

    <script language="javascript">
    
    $(function(){
        $('#mylink').click(function(){
            $.get('/ajax/someurl', {linkText: $(this).text()}, function(resp){
               // handle response here
            }, 'json');
        });
    
    });
    
    </script>
    
    <a href="#" id="mylink">This text will be passed along</a>
    

    PHP File:

    $text = $_REQUEST['linkText'];
    // do something with $text here
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集