douruduan8812 2017-03-10 05:29
浏览 43
已采纳

在PHP中选中复选框时更新状态

I have a table create from datatables, how to change the status field when checkbox clicked, default status is 'before' then when checkbox clicked it update to be 'after'(in database field status), then the table reload..

This dataabs for display table

    .....
   foreach ($data as $key) {
    // add new button, checkbox
    $data[$i]['ceklolos'] = '<input type="checkbox"      id_data="'.$data[$i]   ['status_lolos'].'" class="btn btn-primary btnedit" >';
       $i++;
    ...

How the rest of code, when checkbox in each data clicked that data row update from 'before status' (default database) to be 'after status', after that the table reload..

Thank you, Im using datatable and php

  • 写回答

1条回答 默认 最新

  • doupengxie4195 2017-03-10 06:01
    关注

    First, add custom data attribute to your checkboxes

    <input type="checkbox" data-id="'.$data['id'].'" data-status="'.$data['status'].'" ../>
    

    In your javascript,

    // IIFE (Immediately Invoke Function Expressions)
    (function (myapp){
       myapp(window.jQuery, window, document);
    }(function myapp($, window, document){
       // $ is now locally scoped
       $(function (){
          // dom is now ready
          var dtTable = $("#sometable").DataTable();
    
          // dom events
          $(document).on("change", '.btnedit', function (){
              var $this = $(this);
              var id = $this.attr("data-id");
              var status = $this.attr("data-status");
    
              // send ajax request 
              $.ajax({
                 url: 'path-to-your-php-file',
                 type: 'post',
                 dataType: 'json',
                 data: {
                    id: id, 
                    status: status
                 },
                 beforeSend: function (){
                   // do something here before sending ajax
                 },
                 success: function (data){
                   // do something here
                   if( data.success ){
                      // update your table, or any html dom you want here
                      // if you want to add/remove rows from your dataTable,
                      // you can refer here 
                      // https://datatables.net/reference/api/row.add()
                      // https://datatables.net/reference/api/row().remove()
                      // 
                   }
                 },
                 error: function (data){
                   // do something here if error 
                   // console.warn(data);
                 }
              });
          });
       });
       // The rest of the code goes here
    }));
    

    In your PHP file,

    <?php 
    
    $id = $_POST['id'];
    $status = $_POST['status'];
    // do your update codes here
    // 
    // after successful update return something so in your ajax you
    // will know what happened behind the scene
    $response = array('success' => false, 'msg' => 'Some error message');
    if( some_update_function_success() ){
       $response = array('success' => true, 'msg' => 'Some success message');
    }
    echo json_encode($response);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀