douruduan8812
2017-03-10 05:29
浏览 37

在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

图片转代码服务由CSDN问答提供 功能建议

我有一个从数据表创建的表,如何在单击复选框时更改状态字段,默认状态为'之前' 然后当单击复选框时,它会更新为'after'(在数据库字段状态中),然后表重新加载..

显示表的数据表

  ..... 
 foreach($ data as $ key){
 //添加新按钮,复选框
 $ data [$ i] ['ceklolos'] ='&lt; input type =“ 复选框“id_data =”'。$ data [$ i] ['status_lolos']。'“class =”btn btn-primary btnedit“&gt;'; 
 $ i ++; 
 ... 
    
 
 

当每个数据中的复选框单击该数据行从“之前状态”(默认数据库)更新为“状态之后”时,其余代码如何重新加载...

谢谢,我正在使用datatable和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);
    
    点赞 打赏 评论

相关推荐 更多相似问题