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条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动