3

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

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

它让我进入mysite.com/search/results/处理帖子参数的页面。当然,我可以使用GET方法,然后就可以了

/search/results?keyword="some_keyword",

但是是否可以使结果页面 URL 看起来像

mysite.com/search/results/keyword
4

1 回答 1

4

我会使用 jQuery

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

另一种可能性是在您的控制器中创建一个代理方法,如果您想在 url 中包含所有帖子值,这很有用:

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);
}

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

<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);
于 2012-08-22T09:37:32.947 回答