如何在codeigniter中制作动态表单动作地址? [英] How to make dynamic form action address in codeigniter?
本文介绍了如何在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/
页面,在那里我处理发布参数.当然我可以用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",
但是是否可以使结果页面 URL 看起来像
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());
});
另一种可能是在你的控制器中创建一个代理方法,如果你想在 url 中包含所有的 post 值,它会很有用:
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);
}
在那种情况下,我会在 post 数据中使用数组来简化代码:
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屋!
查看全文