如何使用 codeigniter uri 格式创建搜索表单?
亲爱的大家,我是 codeigniter 的菜鸟,
我想寻求建议,了解如何创建一个搜索表单,该表单将导致 url 为:
http://domain/class/index/searchvar1/searcvar1val/searchvar2/searchvar2val< /a>
表格将类似于
<form action="http://domain/class/index">
<input name="searchvar1" value="searchvar1val">
<input name="searchvar2" value="searchvar2val">
<input type="submit">
</form>
什么是最佳实践?
我已经用谷歌搜索过包括 stackoverflow 帖子,仍然找不到任何解决方案。
谢谢 :)
~thisismyfirstpostthere
更新 ::
我想强调,我的目标是处理来自 uri 字符串的搜索变量(上面),不是来自后变量;所以我认为将表单搜索设置为 POST 不是一个选项? :)
更新 (2)
我不认为这可以在 codeigniter 中开箱即用,我正在考虑将 vars/vals 形式客户端处理为使用 jQuery 的 URI 格式的表单操作。 完成后将在此处发布更新。
dear all, i'm a noob in codeigniter,
i would like to ask for advise, on how to create a search form that would result to url as:
http://domain/class/index/searchvar1/searcvar1val/searchvar2/searchvar2val
the form would be like
<form action="http://domain/class/index">
<input name="searchvar1" value="searchvar1val">
<input name="searchvar2" value="searchvar2val">
<input type="submit">
</form>
what is the best practice for this?
i've googled around including stackoverflow posts, still can't find any solution.
thanks :)
~thisismyfirstposthere
update ::
i would like to emphasize that my objective is to process search variables from the uri string (above), not from post vars; so i think setting the form search to POST is not an option? :)
update (2)
i don't think this can be done out of the box in codeigniter, i'm thinking of client-processing the form vars/vals into the form action in URI format using jQuery.
will post an update here later when done.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 user_guide :
我认为如果您的表单是这样的:
那么在搜索控制器中的某个位置执行此
$this->uri->assoc_to_uri($data);
恕我直言,将生成product/shoes /尺寸/大/颜色/红色
taking a look at the user_guide :
and i think if your form is this:
then somewhere in the search controller do this
$this->uri->assoc_to_uri($data);
IMHO will produceproduct/shoes/size/large/color/red
嘿伙计们,感谢您的回复和讨论,我认为我会顺其自然:
查看 -->形式--> jquery,在表单提交时:用表单参数替换操作属性 -->带有 uri 字符串的控制器 -->流程的其余部分,
jquery 部分将类似于以下
注释:
hey guys, thanks for replying and discussing, I think imma go with flow:
View --> form --> jquery, on form submit: replace action attribute with form params --> controller with uri string --> rest of the process
the jquery part would be something like this
notes:
您可能需要启用查询字符串并使用
$_GET. IMO,这是查询字符串用途的完美示例。
确保将表单更改为
然后,您的 URL 中将包含一个查询字符串,例如
?searchvar1=value1&searchvar2=value2
您可以像这样访问字符串的一部分:
$this->input->get('searchvar1')
使用您的方法当前使用中,如果用户搜索
$config['permissed_uri_chars']
中不允许的任何字符,您将必须自己进行大量编码和解码以确保字符串安全/在您的网址中可读。 CI 输入类会为您处理这个问题,所以这是我的建议。基于 URI 段的搜索参数的另一个缺点是没有正确的方式来表达像
?values[]=one&values[]=two&values[]=三 这样的数组代码>.有一些技巧可以解决这个问题,但它们都只是:技巧。你最终会遇到麻烦。
使用 $_POST 非常繁琐,并且强制使用表单提交来获取分页结果和链接,并且还确保您无法链接到搜索结果或正确使用后退按钮。
如果您必须使用此方法,只需将表单发布到您的控制器,读取输入,然后
redirect()
到您从中构造的URL。 Littlechad 在 他的答案要到达此网址,您可以对表单帖子执行类似的操作(在帖子后的控制器中):
我相信您可以看到这比仅仅使用尝试过的工作量要多得多- and-true 查询字符串...
You may want to enable query strings and use
$_GET
. IMO, this is a perfect example what the query string is intended for.Make sure to change your form to
<form method="get" action="http://domain/class/index">
You will then have a query string in your URL like
?searchvar1=value1&searchvar2=value2
You can access parts of the string like this:
$this->input->get('searchvar1')
With the method you are currently using, if a user searches for any characters that are not allowed in your
$config['permitted_uri_chars']
, you will have to do a lot of encoding and decoding yourself to make sure the string is safe/readable in your URL. The CI Input class takes care of this for you, so it is my recommendation.Another disadvantage to URI segment based search params is that there is no proper way to express an array like
?values[]=one&values[]=two&values[]=three
. There are hacks to work around this, but they are all exactly that: hacks. you will eventually run into trouble.Using $_POST is very tedious and forces the use of form submits for paginated results and links, and also ensures that you cannot link to the search results or use the back button properly.
If you must use this method, simply have the form post to your controller, read the input, and then
redirect()
to the url that you construct from it. littlechad has the right idea for this method in his answerTo get TO this url, you can do something like this with your form post (in the controller after post):
I'm sure you can see how much more work this is than just using the tried-and-true query string...