php Codeigniter - 从下拉菜单中获取值到控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16087560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter - Grab value from dropdown pass to controller
提问by Hashey100
I am trying to pass a value from a form which has 3 options so when the user clicks on any of the options it should pass the value to the controller.
我试图从具有 3 个选项的表单传递一个值,因此当用户单击任何选项时,它应该将该值传递给控制器。
I am thinking maybe i can have something like `onChange="selectedValue". I tried to grab the post from the view but it did not work.
我在想也许我可以有类似 `onChange="selectedValue" 的东西。我试图从视图中抓取帖子,但没有用。
Any help would be appreciated
任何帮助,将不胜感激
View
看法
<label>Reports</label>
<form>
<select NAME="hours">
<option value="24">24</option>
<option value="12">12</option>
<option value="1">1</option>
</select>
</form>
Controller
控制器
public function about()
{
$search = $this->input->post('hours');
echo $search;
$this->load->view("about");
}
回答by jleft
You'd normally pass a value from a view to a controller, by sumitting the form in the view to a function in a controller. You can add JavaScript to the form to automatically submit it when an option is selected - onchange="this.form.submit()"
. (Rather than using a button, for example.)
您通常会将值从视图传递给控制器,方法是将视图中的表单与控制器中的函数相加。您可以将 JavaScript 添加到表单中以在选择选项时自动提交 - onchange="this.form.submit()"
。(例如,而不是使用按钮。)
View
看法
<form method="post" accept-charset="utf-8" action="<?php echo site_url("yourcontroller/about"); ?>">
<select name="hours" onchange="this.form.submit()">
<option value="24">24</option>
<option value="12">12</option>
<option value="1">1</option>
</select>
</form>
Controller
控制器
public function about()
{
//Get the value from the form.
$search = $this->input->post('hours');
//Put the value in an array to pass to the view.
$view_data['search'] = $search;
//Pass to the value to the view. Access it as '$search' in the view.
$this->load->view("about", $view_data);
}