dpjhq00684 2018-09-21 10:23
浏览 119

使用ajax,php在同一页面上获取id

Introduction: I am doing a server-side datatables library using PHP and Ajax. When a user clicks the edit button I want to transfer that id to PHP so I can later update it...

I am basically doing these two stack-overflow articles combined:

Index.php

<script type="text/javascript"> 
$(document).on('click','.edit_btn',function (){
var edit_id = $(this).attr("id");
alert(edit_id);
    $.ajax({
         url: 'index.php',
      data: { edit_id : edit_id },
        contentType: 'application/json; charset=utf-8',
        success: function(result) {
              alert(result);
        }
    });
});
</script>

Edit button code:

columnDefs:
[
    {  
        targets: -1,
        render: function (data, type, row, meta) {
            return '<input type="button" class="delete_btn" id=n-"' + meta.row + '" value="delete"/> <input type="button" class="edit_btn" id=s-"' + meta.row + '" value="edit" name="edit_btn"> </a>';
        }
    }
]

I believe datatables library does not recommend using the get method from the URL. What am I missing? Thanks.

  • 写回答

1条回答 默认 最新

  • dsm17496 2018-09-21 10:59
    关注

    After reading the comments, I got this final solution to work:

     <script type="text/javascript"> 
        $(document).on('click','.edit_btn',function (){
              var id = $(this).attr("id").match(/\d+/)[0];
                var edit_id = $('#example').DataTable().row( id ).data();
                var edit_id = edit_id[0];
        alert(edit_id);
            $.ajax({
                 url: 'index.php',
              data: { edit_id : edit_id },
                contentType: 'application/json; charset=utf-8',
                success: function(result) {
                      //alert(result);
                }
            });
        });
        </script>
    

    Thank you to all who helped!

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀