【问题标题】:Post the id on delete in codeigniter在codeigniter中发布删除id
【发布时间】:2019-01-01 13:49:26
【问题描述】:

我有两个表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>

【问题讨论】:

  • HTML 旁注:&lt;form&gt; 不能成为 &lt;table&gt; 的子级。

标签: php forms codeigniter session codeigniter-3


【解决方案1】:

有很多方法可以做到这一点,最常见的一种是从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。

【讨论】:

    【解决方案2】:

    删除带有确认的链接

    <a href="javascript:;" class="btn btn-danger" onclick="delete('<?php echo $post['id'] ?> ')">Delete</a>
    

    在js中调用delete方法,然后显示确认信息。

    【讨论】:

      【解决方案3】:

      为什么要使用form 标签来调用函数?您可以使用 锚标记 轻松完成此操作,并且可以传递 id。这一行代码将完美运行

      <td><a class="btn btn-danger" href="<?= site_url('posts/delete/'.$post['id']) ?>">Delete</a></td>
      

      在控制器delete函数中会是这样的

      public function delete($id){
         echo $id;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-06
        • 2015-09-05
        • 2016-05-24
        • 2013-07-15
        • 2013-04-19
        • 2012-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多