Reputation: 984
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.
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.
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");
}
Upvotes: 3
Views: 24592
Reputation: 3457
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.)
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);
}
Upvotes: 4