codeigniter表单没有获得正确的用户ID

时间:2015-03-26 11:19:28

标签: codeigniter

我有一个包含我所有用户的表格,我有一个按钮,我可以点击它并加载bootstrap模式,bootstrap模式有一个表单。

但由于某种原因,form_open所有方式都显示了表格中的最后一个ID。

http://localhost/project/admin/users/delete_user/17

如何点击每个删除按钮,模型上的表单将显示正确的ID。

<?php echo Modules::run('admin/common/header/index');?>
<div id="wrapper">
<?php echo Modules::run('admin/common/menu/index');?>
<div id="page-wrapper">
<div id="page-inner">

<div class="panel panel-default">
<div class="panel-heading clearfix">
    <div class="pull-left" style="padding-top: 7.5px"><h1 class="panel-title"><i class="fa fa-list"></i> <?php echo $title;?></h1></div>
    <div class="pull-right">
        <a href="<?php echo base_url('admin/users/add');?>" role="button" class="btn btn-success"><i class="fa fa-plus"></i> Add</a>
    </div>
</div>
<div class="panel-body">
<?php echo Modules::run('admin/message/messages/index');?>
<div class="table-responsive">
<table  class="table table-striped table-bordered table-hover">
    <thead>
        <tr>
            <th>User ID</th>
            <th>Username</th>
            <th class="text-right">User Status</th>
            <th class="text-right">Date Registered</th>
            <th class="text-right">Update User Info</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($users as $user) { ?> 
        <tr>
            <td><?php echo $user['user_id'];?></td>
            <td><?php echo $user['username'];?></td>
            <td class="text-right"><?php echo $user['status'];?></td>
            <td class="text-right"><?php echo $user['date_added'];?></td>
            <td class="text-right">
            <a data-toggle="modal" data-target="#myModal" role="button" class="btn btn-danger"><i class="fa fa-trash"></i> Remove User</a>
            <a href="<?php echo $user['update_user_info'];?>" role="button" class="btn btn-primary">Update User Info</a>
            </td>
        </tr>
    <?php } ?>
    </tbody>
</table>
</div>
</div>
</div>

<!-- Modal -->
<?php echo form_open_multipart('admin/users/delete_user' .'/'.  $user['user_id']);?>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">Confrm Delete Notice</h4>
</div>
<div class="modal-body">
<div class="alert alert-danger text-center">
<h4>You are about to delete this user. <?php echo $user['user_id'];?></h4>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Confirm Delete</button>
</div>
</div>
</div>
</div>
<?php echo form_close();?>


</div><!-- # Page Inner End  -->
</div><!-- # Page Wrapper End  -->
</div><!-- #.WRAPPER  -->
<?php echo Modules::run('admin/common/footer/index');?>
<script type="text/javascript">
$('#myModal').on('shown.bs.modal', function () {
  $('#myInput').focus()
});

更新

表格循环

http://localhost/project/admin/users/delete_user/15弹出的每个模型都没有显示正确的ID。

<?php echo Modules::run('admin/common/header/index');?>
<div id="wrapper">
<?php echo Modules::run('admin/common/menu/index');?>
<div id="page-wrapper">
<div id="page-inner">

<div class="panel panel-default">
<div class="panel-heading clearfix">
    <div class="pull-left" style="padding-top: 7.5px"><h1 class="panel-title"><i class="fa fa-list"></i> <?php echo $title;?></h1></div>
    <div class="pull-right">
        <a href="<?php echo base_url('admin/users/add');?>" role="button" class="btn btn-success"><i class="fa fa-plus"></i> Add</a>
    </div>
</div>
<div class="panel-body">
<?php echo Modules::run('admin/message/messages/index');?>
<div class="table-responsive">
<table  class="table table-striped table-bordered table-hover">
    <thead>
        <tr>
            <th>User ID</th>
            <th>Username</th>
            <th class="text-right">User Status</th>
            <th class="text-right">Date Registered</th>
            <th class="text-right">Update User Info</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($users as $user) { ?> 
        <tr>
            <td><?php echo $user['user_id'];?></td>
            <td><?php echo $user['username'];?></td>
            <td class="text-right"><?php echo $user['status'];?></td>
            <td class="text-right"><?php echo $user['date_added'];?></td>
            <td class="text-right">
            <a data-toggle="modal" data-target="#myModal" role="button" class="btn btn-danger"><i class="fa fa-trash"></i> Remove User</a>
            <a href="<?php echo $user['update_user_info'];?>" role="button" class="btn btn-primary">Update User Info</a>
            </td>
        </tr>
        <!-- Modal -->
        <?php echo form_open_multipart('admin/users/delete_user' .'/'.  $user['user_id']);?>
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
        <div class="modal-content">
        <div class="modal-header">
        <h4 class="modal-title" id="myModalLabel">Confirm Delete Notice</h4>
        </div>
        <div class="modal-body">
        <div class="alert alert-danger text-center">
        <h4>You are about to delete this user. <?php echo $user['user_id'];?></h4>
        </div>
        </div>
        <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Confirm Delete</button>
        </div>
        </div>
        </div>
        </div>
        <?php echo form_close();?>
    <?php } ?>
    </tbody>
</table>
</div>
</div>
</div>
</div><!-- # Page Inner End  -->
</div><!-- # Page Wrapper End  -->
</div><!-- #.WRAPPER  -->
<?php echo Modules::run('admin/common/footer/index');?>
<script type="text/javascript">
$('#myModal').on('shown.bs.modal', function () {
  $('#myInput').focus()
});
</script>

2 个答案:

答案 0 :(得分:1)

将它与Table循环分开:

<table  class="table table-striped table-bordered table-hover">
<thead>
    <tr>
        <th>User ID</th>
        <th>Username</th>
        <th class="text-right">User Status</th>
        <th class="text-right">Date Registered</th>
        <th class="text-right">Update User Info</th>
    </tr>
</thead>
<tbody>
<?php foreach ($users as $user) { ?> 
    <tr>
        <td><?php echo $user['user_id'];?></td>
        <td><?php echo $user['username'];?></td>
        <td class="text-right"><?php echo $user['status'];?></td>
        <td class="text-right"><?php echo $user['date_added'];?></td>
        <td class="text-right">
        <a data-toggle="modal" data-target="#myModal<?php echo $user['user_id'];?>" role="button" class="btn btn-danger"><i class="fa fa-trash"></i> Remove User</a>
        <a href="<?php echo $user['update_user_info'];?>" role="button" class="btn btn-primary">Update User Info</a>
        </td>
    </tr>
    <?php echo form_close();?>
<?php } ?>
</tbody>
</table>

// Separate from Table
<?php foreach ($users as $user) { ?> 
<?php echo form_open_multipart('admin/users/delete_user' .'/'.  $user['user_id']);?>
    <div class="modal fade" id="myModal<?php echo $user['user_id'];?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
    <div class="modal-content">
    <div class="modal-header">
    <h4 class="modal-title" id="myModalLabel">Confirm Delete Notice</h4>
    </div>
    <div class="modal-body">
    <div class="alert alert-danger text-center">
    <h4>You are about to delete this user. <?php echo $user['user_id'];?></h4>
    </div>
    </div>
    <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="submit" class="btn btn-primary">Confirm Delete</button>
    </div>
    </div>
    </div>
    </div>
    <?php echo form_close();?>
<?php } ?>

还为每个模型窗口data-target="#myModal<?php echo $user['user_id'];?>"id="myModal<?php echo $user['user_id'];?>"

分别显示模型ID

答案 1 :(得分:1)

在你的td中设置标签href,

    <td>
         <a href="javascript:void(0)" id='<?php echo $user['user_id'] ?>' data-toggle="tooltip" class="btn btn-default" data-original-title="Delete" onclick="delete_user($(this))" title="Delete User">
              <i class="fa fa-trash-o"></i>
         </a> 
</td>

并将js中的delete函数设置为delete_user(element)并获取user_id之类,

var user_id = element.attr("id");