php codeigniter 中的 form_open
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9457019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
form_open in codeigniter
提问by Mike Rifgin
I have a url that looks like this:
我有一个看起来像这样的网址:
http://localhost/store/mens/category/t-shirts/item/a-t-shirt
http://localhost/store/mens/category/t-shirts/item/at-shirt
I have a class called store and at this point in my application the item function has been called and some data about a product has been output on the page.
我有一个名为 store 的类,此时在我的应用程序中,item 函数已被调用,并且有关产品的一些数据已输出到页面上。
I need to allow a user to add the item to a basket. I know CI provides a library to help with this and I've built a simple class that will interact with this library to create the shopping cart functionality. Problem is I don't understand how I'm supposed to get the form to submit to my shopping cart class and then return to the current url with all the parameters intact like above. Using:
我需要允许用户将项目添加到购物篮中。我知道 CI 提供了一个库来帮助解决这个问题,我已经构建了一个简单的类,它将与这个库交互以创建购物车功能。问题是我不明白我应该如何让表单提交到我的购物车类,然后返回到当前的 url,所有参数都完好如上。使用:
<?= form_open('cart/addItem',array('class' => 'basketForm')); ?>
submits to the correct class but then I have no mechanism to get back to the product page afterwards.
提交给正确的课程,但之后我没有机制返回产品页面。
The only way I can think to do it is to send the url along to the cart class and redirect once the cart stuff is done....or use AJAX...but both seem like hacks to get this working.
我能想到的唯一方法是将 url 发送到购物车类并在购物车完成后重定向....或使用 AJAX...但两者似乎都是让这个工作的黑客。
Is there a clean way to do this?
有没有干净的方法来做到这一点?
回答by landons
Redirect to the referrer page with one of two approaches:
使用以下两种方法之一重定向到引用页面:
1. In the Controller only:
1. 仅在控制器中:
<?php
class Cart extends CI_Controller {
public function addItem()
{
// ... add to cart here
redirect($_SERVER['HTTP_REFERER']);
}
}
2. From the view, tell the controller where you want it to go after:
2. 从视图中,告诉控制器你想要它去哪里:
<!-- Form view //-->
<?= form_open('cart/addItem',array('class' => 'basketForm')); ?>
<?= form_hidden('next_URI', current_url()); // requires URL_helper ?>
...
<?= form_submit('', 'Add to Cart'); ?>
<?= form_close(); ?>
<?php
// Controller
class Cart extends CI_Controller {
public function addItem()
{
// ... add to cart here
redirect($this->input->post('next_URI'));
}
}