doudi5524 2014-10-23 14:40
浏览 62

使用Onclick调用PHP链接

This isn't a duplicate question by any means and I have tried a lot finding solutions.So, please read it before down voting.

enter image description here

Background:

This application is like a note-taking web app where you can post/delete your notes.

Each item in the list has an id which is needed when making a delete call.

In my application, I have to delete individual items from a list which is generated by looping over a JSON response (by a REST API) using PHP.The JSON response can be obtained after successful login.

Question:

To implement delete functionality I have to send id of each of the items as a parameter to the rest api delete call.

So, for this I have to generate dynamic links of the form :

http://localhost/myfolder/api/notes/:id 

which should be passed to the delete.php function (Which I have implemented in CURL).

I searched for possible ways :

  1. Using a PHP function: It seems to be complex, however if there is some way to invoke a PHP function (the delete code using CURL) on click of a link (Which I found not possible as per some answers ?) this could be a great solution.

  2. Using Javascript: I have to call a function upon click of link that sets a variable $_SESSION["id"] to the current item["id"] and then goes to delete.php where I use the $_SESSION variable to first set up the link and then use the CURL code.

I tried basic implementation using the second approach but I have hit a roadblock in this issue. It would be great if you could tell with a bit of code which approach should be followed or any other way to do this ?

This functionality is present in twitter/facebook and almost every such service, how do they implement this, the basic approach should be the same, right: Generate dynamic links and pass them to a php script on click ?

Basic Javasript approach :

   <script>
    <script>
    var el = document.getElementById('del1');
    el.onclick = del1;


    function del() {
      // I have to set $_SESSION here
      return false;
    }
    </script>

   echo "<a href=\"delete.php\" title=\"Delete\" id=\"del1\">"; 
   //Here, I have to pass the item["id"] to the javascript function. 

I had tried some other ways but I have modified the code a lot so, I can't post them. Thanks for your help.

  • 写回答

1条回答 默认 最新

  • drtwqc3744 2014-10-23 14:49
    关注

    Regarding #2, you can't access the user's session from Javascript, so that will not work.

    My preferred way (if using jquery) is to put the id in a data attribute of the delete button (or the block as a whole). Then in the delete onclick function do something like

    <div class="block" data-itemid="<?=$item['id']?>">
        ...
        <div class="delete_button">Delete</div>
    </div>
    

    ...

    $('.delete_button').on('click',function(event) {
        block = $(event).target.parent('.block');
        itemid = block.data('itemid');
        $.post('delete.php',[itemid: itemid]...);
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么