【问题标题】:how to redirect to the same page after editing data?编辑数据后如何重定向到同一页面?
【发布时间】:2018-03-11 03:17:55
【问题描述】:

老师。你能帮我写代码吗?我想在编辑后将页面重定向到同一页面。

这是我的控制器

function edit_book(){
$editBook = array(
    'id' => $this->input->post('id'),
    'book_title' => $this->input->post('book_title'),
    'category' => $this->input->post('category'),
    'author' => $this->input->post('author')
    ):
    $this->m_profile->edit_book($data1, $data, 'book_data');
    redirect('app/book')
}

假设我正在编辑第 3 页的数据。提交编辑后的数据后如何再次重定向到第 3 页(app/book/3)?

我试图通过获取 URI 值来解决它(假设页面是 app/book/3)

$url =  "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
$query_str = parse_url($escaped_url, PHP_URL_QUERY);
parse_str($query_str, $query_params);
print_r($query_params);

但它会导致 array() 或 null。任何人都可以帮助我。谢谢

【问题讨论】:

  • 你试过$_SERVER['HTTP_REFERER']吗?
  • 你可以在 CI 中使用$config[‘uri_segment’] 得到3
  • 是的,它显示了我的 uri。我需要我最后一个 uri 段的值(3)我从哪里得到它? @Cashbee
  • 你在哪里重定向你的页面edit_book或book请解释一下

标签: php codeigniter pagination


【解决方案1】:

在控制器中需要将最后一个值如“3”作为参数传递,以便任何编辑的值都可以传递给控制器​​。然后在编辑代码后,您可以通过 php 标头函数将传递的值作为参数重定向到同一页面上,如下例所示:

<?php     
function edit_book($id){

Your all edited codes here.

// To redirect to the same page with avalue
redirect('same_controller/method/'.$id, 'refresh');
}
?>

【讨论】:

  • 帖子本身并没有真正指定,但标签上写着codeigniter,语法看起来像是使用codeigniter的功能。它还具有redirect() 功能。为什么要改用header()?也不需要完整的硬编码 URL。为什么die() 在那里?
  • 好的,对于codigniter,需要使用重定向功能,需要在URL的路由上传递id。
【解决方案2】:
function edit_book(){
        $id = $this->input->post('id');
    $editBook = array(
        'id' => $this->input->post('id'),
        'book_title' => $this->input->post('book_title'),
        'category' => $this->input->post('category'),
        'author' => $this->input->post('author')
        ):
        $this->m_profile->edit_book($data1, $data, 'book_data');
        $url = base_url('app/book/'.$id);
        redirect( $url)
    }

    function book($id = NULL)
    {
        //get record using $id;

    }

试试下面的代码我来解决你的问题。我在 url 中传递了 id。

【讨论】:

  • $id 不是分页 ID。再次阅读问题。先理解再给出答案。不要轻易放弃答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-17
相关资源
最近更新 更多