在 CodeIgniter 中发送数据和重定向 [英] Sending data along with a redirect in CodeIgniter

查看:29
本文介绍了在 CodeIgniter 中发送数据和重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 C(CRUD)函数,我想发送一条消息(错误或成功)以及我编写的插入"函数的重定向.有没有办法通过重定向来遵守 POST 字段?

I have a simple C (of CRUD) function, and I'd like to send a message (error or success) along with my redirect from the "insert" function I have written up. Is there a way to adhere a POST field with a redirect?

在伪代码中我有:

function view_all{
    //set up some initial variables
    $this->load->view(viewing_page, $data)
}

function insert{
    if ($this->db->insert(my_table, $_POST)){
        $message = "All's well";
    }
    else {
        $message = "whoops!";
    }
    redirect(view_all);
}

因此,理想情况下,viewing_page 应该有类似

So the viewing_page ideally would have something like

if (isset($message)){
    echo $message
}

所以在第一次通过时,我没有看到任何消息,并且当/如果有插入,它会弹出与消息相同的页面.谢谢!

So on the first time through, I don't see any message, and when/if there's an insert, it pops up the same page with the message. Thanks!

推荐答案

我相信 redirect 使用 header().如果是这样,我不相信您可以将数据与位置标头一起发送.您可以使用会话变量或(不太好)将查询字符串附加到位置 URL 来完成相同的操作.

I believe redirect uses header(). If so, I don't believe you can send data along with a location header. You could accomplish the same thing using session vars or (not as good) appending a query string to the location URL.

对于在 CodeIgniter 中执行此操作的可接受"方式,请查看 会话的一半以上类 文档页面.

For an 'accepted' way to do this in CodeIgniter look a little more than halfway down the session class documentation page.

CodeIgniter 支持flashdata"或会话数据,这些数据仅可用于下一个服务器请求,然后会自动清除.这些可能非常有用,通常用于信息或状态消息(例如:记录 2 已删除").

CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared. These can be very useful, and are typically used for informational or status messages (for example: "record 2 deleted").

这个(现已删除 - 这是一个 存档版本) 发布关于快闪信息 涵盖查询字符串和会话 var 方法.

This (now deleted - here's an archived version) post on flash messages covers both the query string and the session var method.

更新:总结一下现在已删除的帖子,它显示了对消息进行网址编码并附加为查询字符串(来自帖子的示例):

Update: To summarize the now deleted post, it showed both urlencoding a message and appending as a query string (example from post):

header('Location: http://www.example.com/index.php?message='.urlencode($message));

并使用两个框架设置flash"变量(帖子中的示例):

And setting a 'flash' variable using two frameworks (example from post):

//Zend Framework
$flashMessenger = $this->_helper->FlashMessenger;
$flashMessenger->setNamespace('actionErrors');
$flashMessenger->addMessage($message);

//CakePHP
$this->Session->setFlash('Your post has been saved.');
$this->redirect('/news/index');

当然你可以直接使用 $_SESSION 做大致相同的事情(我的例子):

Of course you can do roughly the same thing using $_SESSION directly (my example):

//first request
$_SESSION['flash'] = 'This is a simple flash message.';
//next request
$flash = $_SESSION['flash'];
unset($_SESSION['flash']); //flash is one time only

这篇关于在 CodeIgniter 中发送数据和重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆