发送"发布"来自不同DOM的数据数组,包含PHP和DataTables插件

时间:2017-07-04 21:38:25

标签: php datatables

我想知道我的问题是否愚蠢,但我真的尝试过很多事情并没有设法解决这个问题。

我在表单中有大约200行的动态表,每行的第一列是复选框。

这是表格: enter image description here

我需要通过" POST"发送所有这些信息。方法到另一页。它工作得很好,但我开始有很多行,并决定使用插件DataTables,这太棒了。

问题在于,使用此插件和分页,它只保留我们看到的行的id。如果我在另一个页面中检查另一个ID,则不会保存该帖子。搜索栏也是如此。我在互联网上看了很长时间,因为这个插件只保存了DOM的复选框以便进行优化。但我真的需要在所有页面中检查所有ID。

如果我使用"提交",变量$ _POST [' testID []']只会获取我提交表单时在屏幕上显示的itens。

这是另一页,我获取" testID"

的所有数据

enter image description here

我知道这很简单,但我是Javascript和Datatables的新手。

问题是,即使在使用分页或搜索栏更改DOM之后,我怎样才能获得所有价值。

我知道在ajax和jquery中有解决方案,但我更喜欢php,而且简单的帖子...我对ajax和jQuery非常糟糕:/但是如果你在ajax中有一个解决方案,我将尝试用它来管理。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

为了您的信息,这是我用来解决问题的jquery代码。希望有一天它会帮助某人:):

<!-- The plugin DataTables only saves the checkboxes of the dom. If we want it to save all the checkbox, despite pagination and search, we have to use jquery to create an array with id of the the test selected. We also need this to create a checkbox "all" -->

                <script>
                $(document).ready(function (){
                         var table = $('#table_id5').DataTable({
                            'columnDefs': [
                               {
                                  'targets': 0,
                                  'checkboxes': {
                                     'selectRow': true
                                  }
                               }
                            ],
                            'select': {
                               'style': 'multi'
                            },
                            'order': [[1, 'asc']]
                         });


                         // Handle form submission event
                         $('#frm-example').on('submit', function(e){
                            var form = this;

                            var rows_selected = table.column(0).checkboxes.selected();


                            // Iterate over all selected checkboxes
                            $.each(rows_selected, function(index, rowId){
                               // Create a hidden element
                               $(form).append(
                                   $('<input>')
                                      .attr('type', 'hidden')
                                      .attr('name', 'testID[]')
                                      .val(rowId)
                               );
                            });
                         });
                      });

                </script>