简体   繁体   English

Codeigniter - 从下拉传递到控制器的值

[英]Codeigniter - Grab value from dropdown pass to controller

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". 我想也许我可以拥有像'onChange =“selectedValue”这样的东西。 I tried to grab the post from the view but it did not work. 我试图从视图中抓取帖子,但它没有用。

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

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()" . 您可以在表单中添加JavaScript,以便在选择选项时自动提交它 - onchange="this.form.submit()" (Rather than using a button, for example.) (例如,而不是使用按钮。)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM