数据表中的多选行,如何从JSON获取一列数据到php

Posted

技术标签:

【中文标题】数据表中的多选行,如何从JSON获取一列数据到php【英文标题】:Multiselected rows in datatables, how to get one column data from JSON to php 【发布时间】:2020-07-06 17:06:11 【问题描述】:

我正在获取 jQuery 对象,我想通过 php 传递它。我正在尝试使用数据表插件从填充表的多行中获取一个单元格。

我基本上想选择多个用户并向他们发送电子邮件。我的数据表中有一列是电子邮件。我对此很陌生,尝试了很多东西但没有结果。

所以我可以得到我的 jQuery 对象

$(document).ready(function() 
   var tbl_contacts = $('#tbl_contacts').DataTable( 
        dom: 'Bfrtip',
        buttons:[
            'csv', 'excel', 'pdf', 'print', 'selectAll', 'selectNone'
        ],
        select: 
            style: 'multi'
        ,
        initComplete: function () 
            this.api().columns([2]).every( function () 
                var column = this;
                var select = $('<select><option value="">Tous les syndicats</option></select>')
                    .appendTo( $(column.header()).empty() )
                    .on( 'change', function () 
                        var val = $.fn.dataTable.util.escapeRegex(
                            $(this).val()
                        );

                        column
                            .search( val ? '^'+val+'$' : '', true, false )
                            .draw();
                     );

                column.data().unique().sort().each( function ( d, j ) 
                    select.append( '<option value="'+d+'">'+d+'</option>' )
                 );
             );
                       
     );

    $('#example tbody').on( 'click', 'tr', function () 
        $(this).toggleClass('selected');
     );
    $('#sendemail').click(function()
       var data = tbl_contacts.rows('.selected').data();
        console.log(data);
    );
);

我在控制台中得到了这个:

0: (7) ["1", "2147483647", "test1", "", "", "", ""]

1: (7) ["2", "13570477865e7937023cbe43.26502462", "test some id", "", "", "", ""]

2: (7) ["2", "699674795e793702417b57.37923011", "test some id", "", "", "", ""]

我想通过我的 PHP 文件发布每一行的第二列是用户 ID 来查询它。我只是不知道如何将 JSON 中的值 POST 到 PHP 以便能够遍历选定行的用户 ID。

我的操作按钮是:

<div>                    
<a  id="sendemail" class="btn btn-success float-left" method="post" href="send_email.php">Send email</a>
</div>



Thanks ahead of time for the help.



【问题讨论】:

您能否在您正在使用的插件中添加更多信息(例如链接),以便其他人了解 sn-p 的工作原理? 【参考方案1】:

让我们通过 ajax 将您的数据作为数组发送,您可以通过 PHP 访问:

$(document).ready(function() 
   var tbl_contacts = $('#tbl_contacts').DataTable( 
        dom: 'Bfrtip',
        buttons:[
            'csv', 'excel', 'pdf', 'print', 'selectAll', 'selectNone'
        ],
        select: 
            style: 'multi'
        ,
        initComplete: function () 
            this.api().columns([2]).every( function () 
                var column = this;
                var select = $('<select><option value="">Tous les syndicats</option></select>')
                    .appendTo( $(column.header()).empty() )
                    .on( 'change', function () 
                        var val = $.fn.dataTable.util.escapeRegex(
                            $(this).val()
                        );

                        column
                            .search( val ? '^'+val+'$' : '', true, false )
                            .draw();
                     );

                column.data().unique().sort().each( function ( d, j ) 
                    select.append( '<option value="'+d+'">'+d+'</option>' )
                 );
             );
                       
     );

    $('#tbl_contacts tbody').on( 'click', 'tr', function () 
        $(this).toggleClass('selected');
     );
	

	$('#sendemail').click(function()
		var data = tbl_contacts.rows('.selected').data();
		var arr = $.makeArray( data );

		$.ajax(
			type: "POST",
			data: email:arr,
			url: "send_email.php",

		)
	);

);
table.dataTablewidth:100%;margin:0 auto;clear:both;border-collapse:separate;border-spacing:0table.dataTable thead th,table.dataTable tfoot thfont-weight:boldtable.dataTable thead th,table.dataTable thead tdpadding:10px 18px;border-bottom:1px solid #111table.dataTable thead th:active,table.dataTable thead td:activeoutline:nonetable.dataTable tfoot th,table.dataTable tfoot tdpadding:10px 18px 6px 18px;border-top:1px solid #111table.dataTable thead .sorting,table.dataTable thead .sorting_asc,table.dataTable thead .sorting_desc,table.dataTable thead .sorting_asc_disabled,table.dataTable thead .sorting_desc_disabledcursor:pointer;*cursor:hand;background-repeat:no-repeat;background-position:center righttable.dataTable thead .sortingbackground-image:url("../images/sort_both.png")table.dataTable thead .sorting_ascbackground-image:url("../images/sort_asc.png")table.dataTable thead .sorting_descbackground-image:url("../images/sort_desc.png")table.dataTable thead .sorting_asc_disabledbackground-image:url("../images/sort_asc_disabled.png")table.dataTable thead .sorting_desc_disabledbackground-image:url("../images/sort_desc_disabled.png")table.dataTable tbody trbackground-color:#fffffftable.dataTable tbody tr.selectedbackground-color:#B0BED9table.dataTable tbody th,table.dataTable tbody tdpadding:8px 10pxtable.dataTable.row-border tbody th,table.dataTable.row-border tbody td,table.dataTable.display tbody th,table.dataTable.display tbody tdborder-top:1px solid #dddtable.dataTable.row-border tbody tr:first-child th,table.dataTable.row-border tbody tr:first-child td,table.dataTable.display tbody tr:first-child th,table.dataTable.display tbody tr:first-child tdborder-top:nonetable.dataTable.cell-border tbody th,table.dataTable.cell-border tbody tdborder-top:1px solid #ddd;border-right:1px solid #dddtable.dataTable.cell-border tbody tr th:first-child,table.dataTable.cell-border tbody tr td:first-childborder-left:1px solid #dddtable.dataTable.cell-border tbody tr:first-child th,table.dataTable.cell-border tbody tr:first-child tdborder-top:nonetable.dataTable.stripe tbody tr.odd,table.dataTable.display tbody tr.oddbackground-color:#f9f9f9table.dataTable.stripe tbody tr.odd.selected,table.dataTable.display tbody tr.odd.selectedbackground-color:#acbad4table.dataTable.hover tbody tr:hover,table.dataTable.display tbody tr:hoverbackground-color:#f6f6f6table.dataTable.hover tbody tr:hover.selected,table.dataTable.display tbody tr:hover.selectedbackground-color:#aab7d1table.dataTable.order-column tbody tr&gt;.sorting_1,table.dataTable.order-column tbody tr&gt;.sorting_2,table.dataTable.order-column tbody tr&gt;.sorting_3,table.dataTable.display tbody tr&gt;.sorting_1,table.dataTable.display tbody tr&gt;.sorting_2,table.dataTable.display tbody tr&gt;.sorting_3background-color:#fafafatable.dataTable.order-column tbody tr.selected&gt;.sorting_1,table.dataTable.order-column tbody tr.selected&gt;.sorting_2,table.dataTable.order-column tbody tr.selected&gt;.sorting_3,table.dataTable.display tbody tr.selected&gt;.sorting_1,table.dataTable.display tbody tr.selected&gt;.sorting_2,table.dataTable.display tbody tr.selected&gt;.sorting_3background-color:#acbad5table.dataTable.display tbody tr.odd&gt;.sorting_1,table.dataTable.order-column.stripe tbody tr.odd&gt;.sorting_1background-color:#f1f1f1table.dataTable.display tbody tr.odd&gt;.sorting_2,table.dataTable.order-column.stripe tbody tr.odd&gt;.sorting_2background-color:#f3f3f3table.dataTable.display tbody tr.odd&gt;.sorting_3,table.dataTable.order-column.stripe tbody tr.odd&gt;.sorting_3background-color:whitesmoketable.dataTable.display tbody tr.odd.selected&gt;.sorting_1,table.dataTable.order-column.stripe tbody tr.odd.selected&gt;.sorting_1background-color:#a6b4cdtable.dataTable.display tbody tr.odd.selected&gt;.sorting_2,table.dataTable.order-column.stripe tbody tr.odd.selected&gt;.sorting_2background-color:#a8b5cftable.dataTable.display tbody tr.odd.selected&gt;.sorting_3,table.dataTable.order-column.stripe tbody tr.odd.selected&gt;.sorting_3background-color:#a9b7d1table.dataTable.display tbody tr.even&gt;.sorting_1,table.dataTable.order-column.stripe tbody tr.even&gt;.sorting_1background-color:#fafafatable.dataTable.display tbody tr.even&gt;.sorting_2,table.dataTable.order-column.stripe tbody tr.even&gt;.sorting_2background-color:#fcfcfctable.dataTable.display tbody tr.even&gt;.sorting_3,table.dataTable.order-column.stripe tbody tr.even&gt;.sorting_3background-color:#fefefetable.dataTable.display tbody tr.even.selected&gt;.sorting_1,table.dataTable.order-column.stripe tbody tr.even.selected&gt;.sorting_1background-color:#acbad5table.dataTable.display tbody tr.even.selected&gt;.sorting_2,table.dataTable.order-column.stripe tbody tr.even.selected&gt;.sorting_2background-color:#aebcd6table.dataTable.display tbody tr.even.selected&gt;.sorting_3,table.dataTable.order-column.stripe tbody tr.even.selected&gt;.sorting_3background-color:#afbdd8table.dataTable.display tbody tr:hover&gt;.sorting_1,table.dataTable.order-column.hover tbody tr:hover&gt;.sorting_1background-color:#eaeaeatable.dataTable.display tbody tr:hover&gt;.sorting_2,table.dataTable.order-column.hover tbody tr:hover&gt;.sorting_2background-color:#ececectable.dataTable.display tbody tr:hover&gt;.sorting_3,table.dataTable.order-column.hover tbody tr:hover&gt;.sorting_3background-color:#efefeftable.dataTable.display tbody tr:hover.selected&gt;.sorting_1,table.dataTable.order-column.hover tbody tr:hover.selected&gt;.sorting_1background-color:#a2aec7table.dataTable.display tbody tr:hover.selected&gt;.sorting_2,table.dataTable.order-column.hover tbody tr:hover.selected&gt;.sorting_2background-color:#a3b0c9table.dataTable.display tbody tr:hover.selected&gt;.sorting_3,table.dataTable.order-column.hover tbody tr:hover.selected&gt;.sorting_3background-color:#a5b2cbtable.dataTable.no-footerborder-bottom:1px solid #111table.dataTable.nowrap th,table.dataTable.nowrap tdwhite-space:nowraptable.dataTable.compact thead th,table.dataTable.compact thead tdpadding:4px 17px 4px 4pxtable.dataTable.compact tfoot th,table.dataTable.compact tfoot tdpadding:4pxtable.dataTable.compact tbody th,table.dataTable.compact tbody tdpadding:4pxtable.dataTable th.dt-left,table.dataTable td.dt-lefttext-align:lefttable.dataTable th.dt-center,table.dataTable td.dt-center,table.dataTable td.dataTables_emptytext-align:centertable.dataTable th.dt-right,table.dataTable td.dt-righttext-align:righttable.dataTable th.dt-justify,table.dataTable td.dt-justifytext-align:justifytable.dataTable th.dt-nowrap,table.dataTable td.dt-nowrapwhite-space:nowraptable.dataTable thead th.dt-head-left,table.dataTable thead td.dt-head-left,table.dataTable tfoot th.dt-head-left,table.dataTable tfoot td.dt-head-lefttext-align:lefttable.dataTable thead th.dt-head-center,table.dataTable thead td.dt-head-center,table.dataTable tfoot th.dt-head-center,table.dataTable tfoot td.dt-head-centertext-align:centertable.dataTable thead th.dt-head-right,table.dataTable thead td.dt-head-right,table.dataTable tfoot th.dt-head-right,table.dataTable tfoot td.dt-head-righttext-align:righttable.dataTable thead th.dt-head-justify,table.dataTable thead td.dt-head-justify,table.dataTable tfoot th.dt-head-justify,table.dataTable tfoot td.dt-head-justifytext-align:justifytable.dataTable thead th.dt-head-nowrap,table.dataTable thead td.dt-head-nowrap,table.dataTable tfoot th.dt-head-nowrap,table.dataTable tfoot td.dt-head-nowrapwhite-space:nowraptable.dataTable tbody th.dt-body-left,table.dataTable tbody td.dt-body-lefttext-align:lefttable.dataTable tbody th.dt-body-center,table.dataTable tbody td.dt-body-centertext-align:centertable.dataTable tbody th.dt-body-right,table.dataTable tbody td.dt-body-righttext-align:righttable.dataTable tbody th.dt-body-justify,table.dataTable tbody td.dt-body-justifytext-align:justifytable.dataTable tbody th.dt-body-nowrap,table.dataTable tbody td.dt-body-nowrapwhite-space:nowraptable.dataTable,table.dataTable th,table.dataTable tdbox-sizing:content-box.dataTables_wrapperposition:relative;clear:both;*zoom:1;zoom:1.dataTables_wrapper .dataTables_lengthfloat:left.dataTables_wrapper .dataTables_filterfloat:right;text-align:right.dataTables_wrapper .dataTables_filter inputmargin-left:0.5em.dataTables_wrapper .dataTables_infoclear:both;float:left;padding-top:0.755em.dataTables_wrapper .dataTables_paginatefloat:right;text-align:right;padding-top:0.25em.dataTables_wrapper .dataTables_paginate .paginate_buttonbox-sizing:border-box;display:inline-block;min-width:1.5em;padding:0.5em 1em;margin-left:2px;text-align:center;text-decoration:none !important;cursor:pointer;*cursor:hand;color:#333 !important;border:1px solid transparent;border-radius:2px.dataTables_wrapper .dataTables_paginate .paginate_button.current,.dataTables_wrapper .dataTables_paginate .paginate_button.current:hovercolor:#333 !important;border:1px solid #979797;background-color:white;background:-webkit-gradient(linear, left top, left bottom, color-stop(0%, #fff), color-stop(100%, #dcdcdc));background:-webkit-linear-gradient(top, #fff 0%, #dcdcdc 100%);background:-moz-linear-gradient(top, #fff 0%, #dcdcdc 100%);background:-ms-linear-gradient(top, #fff 0%, #dcdcdc 100%);background:-o-linear-gradient(top, #fff 0%, #dcdcdc 100%);background:linear-gradient(to bottom, #fff 0%, #dcdcdc 100%).dataTables_wrapper .dataTables_paginate .paginate_button.disabled,.dataTables_wrapper .dataTables_paginate .paginate_button.disabled:hover,.dataTables_wrapper .dataTables_paginate .paginate_button.disabled:activecursor:default;color:#666 !important;border:1px solid transparent;background:transparent;box-shadow:none.dataTables_wrapper .dataTables_paginate .paginate_button:hovercolor:white !important;border:1px solid #111;background-color:#585858;background:-webkit-gradient(linear, left top, left bottom, color-stop(0%, #585858), color-stop(100%, #111));background:-webkit-linear-gradient(top, #585858 0%, #111 100%);background:-moz-linear-gradient(top, #585858 0%, #111 100%);background:-ms-linear-gradient(top, #585858 0%, #111 100%);background:-o-linear-gradient(top, #585858 0%, #111 100%);background:linear-gradient(to bottom, #585858 0%, #111 100%).dataTables_wrapper .dataTables_paginate .paginate_button:activeoutline:none;background-color:#2b2b2b;background:-webkit-gradient(linear, left top, left bottom, color-stop(0%, #2b2b2b), color-stop(100%, #0c0c0c));background:-webkit-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%);background:-moz-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%);background:-ms-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%);background:-o-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%);background:linear-gradient(to bottom, #2b2b2b 0%, #0c0c0c 100%);box-shadow:inset 0 0 3px #111.dataTables_wrapper .dataTables_paginate .ellipsispadding:0 1em.dataTables_wrapper .dataTables_processingposition:absolute;top:50%;left:50%;width:100%;height:40px;margin-left:-50%;margin-top:-25px;padding-top:20px;text-align:center;font-size:1.2em;background-color:white;background:-webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255,255,255,0)), color-stop(25%, rgba(255,255,255,0.9)), color-stop(75%, rgba(255,255,255,0.9)), color-stop(100%, rgba(255,255,255,0)));background:-webkit-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,0.9) 25%, rgba(255,255,255,0.9) 75%, rgba(255,255,255,0) 100%);background:-moz-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,0.9) 25%, rgba(255,255,255,0.9) 75%, rgba(255,255,255,0) 100%);background:-ms-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,0.9) 25%, rgba(255,255,255,0.9) 75%, rgba(255,255,255,0) 100%);background:-o-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,0.9) 25%, rgba(255,255,255,0.9) 75%, rgba(255,255,255,0) 100%);background:linear-gradient(to right, rgba(255,255,255,0) 0%, rgba(255,255,255,0.9) 25%, rgba(255,255,255,0.9) 75%, rgba(255,255,255,0) 100%).dataTables_wrapper .dataTables_length,.dataTables_wrapper .dataTables_filter,.dataTables_wrapper .dataTables_info,.dataTables_wrapper .dataTables_processing,.dataTables_wrapper .dataTables_paginatecolor:#333.dataTables_wrapper .dataTables_scrollclear:both.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody*margin-top:-1px;-webkit-overflow-scrolling:touch.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody&gt;table&gt;thead&gt;tr&gt;th,.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody&gt;table&gt;thead&gt;tr&gt;td,.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody&gt;table&gt;tbody&gt;tr&gt;th,.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody&gt;table&gt;tbody&gt;tr&gt;tdvertical-align:middle.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody&gt;table&gt;thead&gt;tr&gt;th&gt;div.dataTables_sizing,.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody&gt;table&gt;thead&gt;tr&gt;td&gt;div.dataTables_sizing,.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody&gt;table&gt;tbody&gt;tr&gt;th&gt;div.dataTables_sizing,.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody&gt;table&gt;tbody&gt;tr&gt;td&gt;div.dataTables_sizingheight:0;overflow:hidden;margin:0 !important;padding:0 !important.dataTables_wrapper.no-footer .dataTables_scrollBodyborder-bottom:1px solid #111.dataTables_wrapper.no-footer div.dataTables_scrollHead table.dataTable,.dataTables_wrapper.no-footer div.dataTables_scrollBody&gt;tableborder-bottom:none.dataTables_wrapper:aftervisibility:hidden;display:block;content:"";clear:both;height:0@media screen and (max-width: 767px).dataTables_wrapper .dataTables_info,.dataTables_wrapper .dataTables_paginatefloat:none;text-align:center.dataTables_wrapper .dataTables_paginatemargin-top:0.5em@media screen and (max-width: 640px).dataTables_wrapper .dataTables_length,.dataTables_wrapper .dataTables_filterfloat:none;text-align:center.dataTables_wrapper .dataTables_filtermargin-top:0.5em
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/select/1.3.1/js/dataTables.select.min.js"></script>

<body>
<table data-order='[[ 0, "asc" ]]' id="tbl_contacts" class="display" cellspacing="0" >
        <thead>
            <tr>
                <th>Numero</th>
                <th>Nom Prenom</th>
                <th>Adresse</th>
                <th>Email</th>
                <th>Code postal</th>
                <th>Commune</th>
            </tr>
        </thead>
        <tbody>
			<tr><td></td><td></td><td></td><td>mail1@zen</td><td></td><td></td></tr>
			<tr><td></td><td></td><td></td><td>mail2@zen</td><td></td><td></td></tr>
		</tbody>
</table>
<div>                    
<a  id="sendemail" class="btn btn-success float-left" href="#">Send email</a>
</div>
</body>

PHP 代码很简单:

<?php
foreach($_POST['email'] as $ligne) 
    echo $ligne[3]."\n";
    //Here comes the send mail function.

请注意,&lt;a&gt; tag 不知道 method 属性。

【讨论】:

似乎正在工作,谢谢。我想要解决的问题是如何访问我的 PHP 文件中的数组,这是我的按钮&lt;a id="sendemail" class="btn btn-success float-left" method="post"&gt;Send email&lt;/a&gt; 然后我想我用 isset($_POST['sendmail')) 放置了一个 if satatement。再次感谢您 很高兴我的回答能帮到你。那你可以点击答案旁边的复选标记吗? 你能帮我解决 PHP 方面的问题吗? 我已经更新了我的代码并添加了 PHP 代码来获取邮件列表。 一切正常,所以我添加了.done(function(data) $('#modal_sendemail').modal('show'); $('#emailto').append('&lt;input value="'+data+'"/&gt;'); console.log(data); ) 我的电子邮件地址在我的电子邮件模式中的 中弹出,我可以在其中编写我的消息。非常感谢,您可以节省时间。

以上是关于数据表中的多选行,如何从JSON获取一列数据到php的主要内容,如果未能解决你的问题,请参考以下文章

度量快速开发平台网格勾选行(标识行),多选行获取方法

如何提取 DataTables 中所选行的第一列?

如何从 GridPanel 的选定行中获取值? [外网]

js 解析layui数据列表的多选状态下的json数据

及时从数据库中取得数据填放进Form表单的多选框中

SELECT2 4.0 与 ASP.Net 中的多选:如何获取和设置所选项目?