如何创建搜索表单到编码器uri格式


how to create search form with into codeigniter uri format?

亲爱的,我是一个编码新手,

我想征求意见,关于如何创建一个搜索表单,结果url为:

http://domain/class/index/searchvar1/searcvar1val/searchvar2/searchvar2val

形式应该是

<form action="http://domain/class/index">
   <input name="searchvar1" value="searchvar1val">
   <input name="searchvar2" value="searchvar2val">
   <input type="submit">
</form>

这方面的最佳实践是什么?

我已经到处搜索了,包括stackoverflow帖子,仍然找不到任何解决方案。

谢谢:)

~ thisismyfirstposthere

update::

我想强调的是,我的目标是从uri字符串(上面)处理搜索变量,而不是从post变量;所以我认为设置表单搜索POST是不是一个选项?:)

更新(2)

我不认为这可以在codeigniter中开箱即用,我正在考虑使用jQuery以URI格式处理表单var/val到表单动作的客户端。完成后会在这里发布更新

查看user_guide:

$this->uri->assoc_to_uri()

接受关联数组作为输入并从中生成一个URI字符串。数组键将包含在字符串。例子:

$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');
$str = $this->uri->assoc_to_uri($array);
// Produces: product/shoes/size/large/color/red 

,我想如果你的形式是这样的:

<form action="http://domain/class/index/search">
   <input name="product" value="shoes">
   <input name="size" value="large">
   <input name="color" value="red">
   <input type="submit">
</form>

然后在搜索控制器的某个地方执行此操作$this->uri->assoc_to_uri($data); IMHO将生成product/shoes/size/large/color/red

大家好,谢谢你们的回复和讨论,我想我还是顺其自然吧:

View -> form -> jquery,在表单提交时:将action属性替换为表单参数->控制器替换为uri字符串->过程的其余部分

jquery部分应该是这样的

$("#searchFormID").submit(function(){
    // $(this).serialize() outputs param1=value1&param2=value2 string
    // var.replace(regex, string) outputs param1/value1/param2/value2 string
    // newact would be http://localhost/class/index/param1/value1/param2/value2
    var newact = $(this).attr("action") + "/" + $(this).serialize().replace(/&|=/g,"/");
    $(this).attr("action", newact); // or $(this).attr("target", newact);
    return true;
});

指出:

  • 为jquery的表单选择器添加一个id属性
  • 在表单中使用post方法(这在codeigniter中是默认的)->所以action url不会被GET字符串
  • 填充

您可能需要启用查询字符串并使用$_GET。在我看来,这是一个查询字符串的完美示例。

确保将窗体更改为<form method="get" action="http://domain/class/index">

你将有一个查询字符串在你的URL,如?searchvar1=value1&searchvar2=value2

您可以像这样访问字符串的部分:$this->input->get('searchvar1')

使用您当前使用的方法,如果用户搜索您的$config['permitted_uri_chars']中不允许的任何字符,您将不得不自己进行大量编码和解码,以确保字符串在您的URL中是安全/可读的。CI Input类会为您处理这个问题,所以这是我的推荐。

基于URI段的搜索参数的另一个缺点是没有合适的方式来表示像?values[]=one&values[]=two&values[]=three这样的数组。有一些黑客可以解决这个问题,但它们都是黑客。你最终会遇到麻烦的。

使用$_POST非常繁琐,并且强制使用表单提交分页结果和链接,并且还确保您无法链接到搜索结果或正确使用后退按钮。

如果您必须使用此方法,只需将表单发送到控制器,读取输入,然后将redirect()发送到从中构建的url。littlehad在他的回答中对这种方法有正确的想法

// This URL:
http://domain/class/index/searchvar1/searcvar1val/searchvar2/searchvar2val
// With this:
$search = $this->uri->uri_to_assoc();
// Produces this:
array(
    'searchvar1'    =>    'searchvarval1',
    'searchvar2'    =>    'searchvarval2',
);
// And you can use it like this:
$arg1 = $search['searchvar1'];
$arg2 = $search['searchvar2'];

要获取这个url,你可以用你的表单post(在post后的控制器中)做这样的事情:

$var1 = $this->input->post('searchvar1');
$var2 = $this->input->post('searchvar2');
redirect('searchvar1/'.urlencode($var1).'/searchvar2/'.urlencode($var2));

我相信你可以看到这比仅仅使用久经考验的查询字符串要多做多少工作…