【问题标题】:Update status when checkbox clicked in PHP在 PHP 中单击复选框时更新状态
【发布时间】:2017-03-10 05:29:18
【问题描述】:

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

这个dataabs用于显示表格

    .....
   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++;
    ...

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

谢谢,我用的是datatable和php

【问题讨论】:

  • 能否提供工作代码
  • PHP 没有可供单击的复选框。它是一种为浏览器生成 HTML 的服务器端语言……一旦浏览器显示页面,它就无法与 PHP 用来生成 HTML 的源代码进行通信——它只是文本,毕竟没有固有的功能.如果您想将数据从浏览器发送到服务器,则需要研究诸如 html 表单之类的概念或更高级的功能,AJAX

标签: javascript php jquery mysql


【解决方案1】:

首先,将自定义数据属性添加到您的复选框

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

在你的 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
}));

在您的 PHP 文件中,

<?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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    相关资源
    最近更新 更多