duancan1900 2013-03-31 17:56
浏览 31
已采纳

动态执行PHP GET

I currently have a php script which is loaded by Javascript when a button is clicked. It creates all the html content which looks like this once loaded

  <ul>
    <li>
        <a href='foo.php?var=abc123'>
    </li>
    <li>
        <a href='foo.php?var=def123'>
    </li>
  </ul>

So when a link is clicked, it uses GET to send PHP variable to same script, and it brings up another list of results.

What I want to do, is when you click a link instead of going to a new page with results, it loads them below this part to result in something like this (like a treeview():

 <ul>
       <li>
          <a href="#" id="abc123">abc123</a>
       </li>
           <ul>
              <li>
                   <a href="#" id="jkl123">jkl123</a>
              </li>
              <li>
                   <a href="#" id="ghi123">ghi123</a>
              </li>
         </ul>
    <li>
        <a href="#" id="def123">def123</a>
    </li>
 </ul>

I can get it to work if I use static variables, but i need to find a way to take the variable from the created html on click, put it into the javascript function, so it can execute the PHP script.

Thanks.

  • 写回答

1条回答 默认 最新

  • dslfjrmz70457 2013-03-31 18:02
    关注

    It sounds like you're going to want to use AJAX:

         $.ajax({
                type: 'GET',
                url: "yoururlhere.com",
                data: {
                    someparam: "somevalue",
                    param2: "anothervalue"
                },
                dataType: "json",
                success: function (JSONdata, textStatus, jqXHR) {
                    // do work here on success
    
    
                }
            });
    

    You can attach it to a click handler like this:

    $("someID").on("click",function(){
    event.preventDefault();
    var href = $(this).attr("href");
    
             $.ajax({
                    type: 'GET',
                    url: href ,
                    data: "param=value&param2=anothervalue",
                    success: function (JSONdata, textStatus, jqXHR) {
                        // do work here on success
    
    
                    }
                });
    });
    

    Update:

    Some example HTML to go along with the above code:

    <a id="someID" href="yoururl.php">Click me</a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c