如何在codeigniter中动态形式动作地址? [英] How to make dynamic form action address in codeigniter?

查看:102
本文介绍了如何在codeigniter中动态形式动作地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站上有一个搜索表单,其格式如下:

I have a search form on my site, and it looks like this:

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

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

It gets me into mysite.com/search/results/ page where I process post parameters. Of course, I can use the GET method, and then it will be

/search/results?keyword="some_keyword",

,但可以使结果页网址看起来像

but is it possible to make the result page URL look like

mysite.com/search/results/keyword


推荐答案

我会使用jQuery

I would use jQuery

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

另一种可能是在控制器中创建一个代理方法,如果你想拥有所有

Another possibility is to make a proxy method in your controller, it's useful if you want to have all your post values in the 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);
}

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

In that case I would use arrays in post data to simplify code:

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

这篇关于如何在codeigniter中动态形式动作地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆