簡體   English   中英

PHP Foreach循環將數據傳遞到jquery模態以供表單使用

[英]PHP Foreach loop passing data onto jquery modal for form use

我在Codeigniter中使用Bootstrap。 我有一張表,可在我正在建立的管理界面上加載帳戶的凍結歷史記錄。 作為foreach一部分的每一行上,如果用戶可以刪除該阻止,則有一個“取消阻止”按鈕,如您希望在以下示例中看到的那樣。 <td><?php if ($bd['expiredate'] > date('Ymd H:i:s')) { ?><button type="button" class="btn btn-info" data-toggle="modal" data-target="#delBlock" <?php if ($check_perm['unbanaccount'] == 0 || $bd['unblock_date'] > 0) { echo "disabled"; } ?> >Unblock</button><?php } ?></td> <td><?php if ($bd['expiredate'] > date('Ymd H:i:s')) { ?><button type="button" class="btn btn-info" data-toggle="modal" data-target="#delBlock" <?php if ($check_perm['unbanaccount'] == 0 || $bd['unblock_date'] > 0) { echo "disabled"; } ?> >Unblock</button><?php } ?></td>

        <div class="table-responsive">
            <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                <thead>
                    <tr>
                        <th style="width: 125px;">Block Date</th>
                        <th style="width: 100px;">Blocked By</th>
                        <th style="width: 125px;">Expiry Date</th>
                        <th style="width: 100px;">Reason</th>
                        <th style="width: 100px;">Unblocked By</th>
                        <th style="width: 125px;">Unblocked Date</th>
                        <th style="width: 30px;">Block<br />Comment</th>
                        <th style="width: 30px;">Unblock<br />Comment</th>
                        <th style="width: 100px;">Options</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($block_list)) { echo "<tr class='odd gradeX'><td colspan='9'><center>No data!</center></td></tr>"; } ?>
                    <?php foreach ($block_list as $bd): ?>
                        <tr class="odd gradeX">
                            <td><?php echo $bd['blockdate']; ?></td>
                            <td><?php echo $bd['blockname']; ?></td>
                            <td><?php echo $bd['expiredate']; ?></td>
                            <td><?php echo $bd['reason']; ?></td>
                            <td><?php echo $bd['ublockname']; ?></td>
                            <td><?php echo $bd['unblock_date']; ?></td>
                            <td><center><a data-toggle="collapse" data-parent="#accordion" href="#blockcomment<?php echo $bd['blockid']; ?>"><button type="button" class="btn btn-primary btn-circle"><i class="fa fa-list"></i></button></a></center></td>
                            <td><?php if (isset($bd['unblock_date']) == TRUE) { ?><center><a data-toggle="collapse" data-parent="#accordion" href="#ublockcomment<?php echo $bd['blockid']; ?>"><button type="button" class="btn btn-primary btn-circle"><i class="fa fa-list"></i></button></a></center><?php } ?></td>
                            <td><?php if ($bd['expiredate'] > date('Y-m-d H:i:s')) { ?><button type="button" class="btn btn-info" data-toggle="modal" data-target="#delBlock" <?php if ($check_perm['unbanaccount'] == 0 || $bd['unblock_date'] > 0) { echo "disabled"; } ?> >Unblock</button><?php } ?></td>
                        </tr>
                        <tr><td colspan="9">
                            <div id="blockcomment<?php echo $bd['blockid']; ?>" class="panel-collapse collapse">
                                <div class="panel-body">
                                    <strong>Block Comment:</strong><br /><?php echo $bd['block_comment']; ?>
                                </div>
                            </div>
                            <div id="ublockcomment<?php echo $bd['blockid']; ?>" class="panel-collapse collapse">
                                <div class="panel-body">
                                    <strong>Unblock Comment:</strong><br /><?php echo $bd['unblock_comment']; ?>
                                </div>
                            </div>
                        </td></tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            <div align="right"><button type="button" class="btn btn-warning" data-toggle="modal" data-target="#addBlock" <?php if ($check_perm['banaccount'] == 0) { echo "disabled"; } ?> >Add New Block</button></div>
        </div>

如果您可以取消阻止帳戶,一旦您單擊“取消阻止”按鈕,就會出現一個模態模式,您可以在其中輸入有關取消阻止用戶的評論。 數據庫中的每個塊都由唯一的ID(在上面的示例中,變量為$bd['blockid']$bd['blockid'] 我需要與該blockid進行通信,並告訴數據庫要更新哪個塊的數據,以表明用戶在表上處於未阻塞狀態。

模態如下:

        <div class="modal fade" id="delBlock" tabindex="-1" role="dialog" aria-labelledby="delBlockLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="delBlockLabel">Remove Block</h4>
                    </div>
                    <div class="modal-body">
                        <?php echo validation_errors(); ?>
                        <?php echo form_open('/account/delblock', array('class' => 'form-inline'), array('blockid' => $bd['blockid'], 'acct_id' => $acct_data->account_id)); ?>
                        <table>
                            <tr><td width="25%"><label>Unblock Comment</label></td>
                            <td width="450px"><textarea class="form-control" name="unbanComments" rows="5" style="width:100%;"></textarea></td></tr>
                        </table>
                    </div>
                    <center><div style="color:#EE0000; ">Note this will remove the ban with immediate effect.</div></center>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary">Remove Block</button>
                    </div>
                    <?php echo form_close(); ?>
                </div>
            </div>
        </div>

如您所見,我正在使用Codeigniter的Form Helper函數通過該表單發布array('blockid' => $bd['blockid'],以便可以使用Codeigniter模型僅處理該記錄。 jQuery模態不在循環中,它始終將最后一條記錄作為$ bd ['blockid']而不是我想要處理的實際記錄(在這種情況下,我的ID為7、6和5(按此順序),我單擊ID為7的記錄,改為5),我需要將特定$ bd ['blockid']的模式傳遞給表單,以便可以在Codeigniter模型中使用它。我將如何完成這樣的事情?

我做了一些工作,將一些事情修補在一起,然后想到了:

我將按鈕更改為以下視圖:

<td><?php if ($bd['expiredate'] > date('Y-m-d H:i:s')) { ?><button type="button" class="btn btn-info" id="delBlockOpen" data-toggle="modal" data-target="#delBlock" data-id="<?php echo $bd["blockid"]; ?>" <?php if ($check_perm['unbanaccount'] == 0 || $bd['unblock_date'] > 0) { echo "disabled"; } ?> >Unblock</button><?php } ?></td>

(添加了data-id="<?php echo $bd["blockid"]; ?>"

然后,在模態中:

<div class="modal fade" id="delBlock" tabindex="-1" role="dialog" aria-labelledby="delBlockLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="delBlockLabel">Remove Block</h4>
            </div>
            <div class="modal-body">
                <?php echo validation_errors(); ?>
                <?php echo form_open('/account/delblock', array('class' => 'form-inline'), array('acct_id' => $acct_data->account_id)); ?>
                <input type="hidden" id="blockidval" name="blockidval" />
                <table>
                    <tr><td width="25%"><label>Unblock Comment</label></td>
                    <td width="450px"><textarea class="form-control" name="unbanComments" rows="5" style="width:100%;"></textarea></td></tr>
                </table>
            </div>
            <center><div style="color:#EE0000; ">Note this will remove the ban with immediate effect.</div></center>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary">Remove Block</button>
            </div>
            <?php echo form_close(); ?>
        </div>
    </div>
</div>

最后一件事,整個過程在我的頁腳中添加了一些附加的JavaScript:

<script type="text/javascript">
  $(function() {
      $(document).on('click','#delBlockOpen',function(e){
          $('#blockidval').val($(this).data('id'));
      });
  });
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM