dongmeijian1716 2017-09-26 02:08
浏览 100
已采纳

如何在没有表单的情况下将数据从HTML传递到PHP?

I'm working with an api that requires specific ID numbers to retrieve certain objects. Right now I have a form for the user to manually enter an ID and on submission it GETs an object. But what I want is to have links to all the IDs instead so the user can just click and choose which object to retrieve. I'm not necessarily trying to make a GET request from the user click. I can make the GET request on the backend but I just need a way to determine which ID the user is trying to select. I have been searching and searching for a way to do this but I haven't found anything specific to my dilemma. I've read on having a hidden form or using AJAX but is there not a simpler solution? Is there not a way to where the PHP server can detect which link and ID is clicked to store it in a variable?

For example:

$lib->request('$ID.'/videos','GET');

retrieves a set of videos.

<button type ="button" id="23456" >Video Number 1</button> Is there a way that I could pass the id of the button above to php?

I'm really confused and I'm wondering if I'm over complicating this.

  • 写回答

2条回答 默认 最新

  • dongshi6529 2017-09-26 05:16
    关注

    you can post data with XHR!

    Here is a simple example using jQuery to post data

    // URL,parameter,callback function,RT datatype
    // just one example
    $.post("/"+id+"/videos",{},
      function(data){
        console.log(data);
      },
      "text");//return datatype:json,html,xml,text
    }
    

    edited

    you can get id by event

    need to know:

    only typeof ID === number triggered

    $(document.body).on("mouseup",function(e){
        var _e  = e.target;
        if(typeof _e.id !== "undefined" && !isNaN(+_e.id)){
             var id = _e.id;
             //trigger XHR last edited code
        }
    })
    

    edited2

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8">
            <title></title>
        </head>
        <body>
            <button type="button" id="123456" class="id-select">Video Number 1</button>
            <button type="button" id="654321" class="id-select">Video Number 2</button><br>
            <script src="path/jquery.js"></script>
            <script>
                $(function(){
                    $(document.body).on("mouseup",function(e){
                        var _e  = e.target;
                        if(typeof _e.id !== "undefined" && !isNaN(+_e.id)){
                            var id = _e.id;  //this id is you click button
                            //trigger XHR last edited code
    
                            /*
                             *   @parma1 url
                             *   @parma2 post parama
                             *   @parma3 callback function
                             *   @parma4 set return datatype
                             */
                            $.post("/"+id+"/videos",{},
                                function(data){
                                console.log(data);
                            }, "text");//return datatype:json,html,xml,text
                         }
                     }
                });
            </script>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度