首页 > 解决方案 > 在codeigniter中发布删除id

问题描述

我有两个表usersposts主键在哪里id。当我点击删除按钮时,我想发布id.

这是我的看法:

<table class="table">
    <thead class="thead-dark">
        <tr>
            <th scope="col">Title</th><th scope="col">Hit</th>
            <th scope="col">Edit</th><th scope="col">Delete</th><th scope="col">Read More</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($posts as $post) : ?>
            <?php if($this->session->userdata('username') ==  $_SESSION["username"]): ?>
                <tr>
                    <td><?php echo $post['title']; ?> </td>
                    <td><?php echo $post['post_views']; ?></td>
                    <td><a class="btn btn-default" href="<?php echo base_url(); ?>posts/edit/<?php echo $post['slug']; ?>">Edit</a></td>
                    <td>
                        <?php echo form_open('/posts/delete/'.$post['id']); ?>
                            <input type="submit" value="Delete" class="btn btn-danger">
                            <input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
                        </form>
                    </td>
                    <td><p><a class="btn btn-default" href="<?php echo site_url('/posts/'.$post['slug']); ?>">Read More</a></p></td>
                </tr>
            <?php endif; ?>
        <?php endforeach; ?>
    </tbody>
</table>

标签: phpformscodeignitersessioncodeigniter-3

解决方案


有很多方法可以做到这一点,最常见的一种是从以下方式获取它url

$id = @end($this->uri->segment_array());

或这个:

$id = $this->uri->segment(3);

或者您可以通过将其传递到隐藏输入中来执行此操作,如下所示:

<?php echo form_open('/posts/delete/'.$post['id']); ?>
    <input type="submit" value="Delete" class="btn btn-danger">
    <input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
</form>

或者使用ajax。


推荐阅读