如何在codeigniter中生成动态表单动作地址


How to make dynamic form action address in codeigniter?

我的网站上有一个搜索表单,它看起来像这样:

<form action="/search/results">
<input type="text" name="keyword">
<button type="submit">  // etc...
</form>

它让我进入mysite.com/search/results/页面,在那里我处理post参数。当然,我可以使用GET方法,然后它将是

/search/results?keyword="some_keyword",

但是有可能使结果页面URL看起来像吗

mysite.com/search/results/keyword

我会使用jQuery

$('#myform').submit(function(){
    $(this).attr('action', $(this).attr('action') + "/" + $(this).find(input[name="keyword"]).val());
});

另一种可能性是在你的控制器中制作一个代理方法,如果你想在url:中拥有所有的post值,这很有用

public function post_proxy() {
    $seg1 = $this->input->post('keyword');
    $seg2 = $this->input->post('keyword2');
    $seg3 = $this->input->post('keyword3');
    redirect('my_method/'.$seg1.'/'.$seg2.'/'.seg3);
}

在这种情况下,我会在post数据中使用数组来简化代码:

<input type="text" name="kw[1]">
<input type="text" name="kw[2]">
<input type="text" name="kw[3]">
$segment_array = $this->input->post('kw');
$segments = implode("/", $segment_array);
redirect('my_method'.$segments);