Dropdown select onChange redirect to url with a parameter in CodeIgniter
if I dont get you wrong, You are asking something like this;
In controller;
public function get_categories()
{
if(isset($_POST) && !empty($_POST())
{
$id = $this->input->post('selectCatsByComponent');
// bla bla bla
$data['id'] = $id;
$this->load->view('whatever/whatever',$data);
}
else
{
//Your normal page before posting
}
}
Now you can access the id value in view like this
if(isset($id)) // if there is a $id loaded
{
// Your view accoring to $id
}
Note: I did not have an test environment while I was writing this code. So The code is not complate and there might be some mistakes
aspirinemaga
Updated on June 04, 2022Comments
-
aspirinemaga almost 2 years
How I would access this url
index.php/backend/categories/get_categories/$id
within a<select>
dropdown onChange ?I mean, if I will go to
index.php/backend/categories/get_categories/1
, it will output a table with all categories undercomponent id 1
.What I want, is once i select an option from a dropdown list, i want it to submit and redirect to the same url, but with a specific ID from
<option val="$id">
?To generate a dropdown list with CodeIgniter FORM helper:
<?php echo form_open('backend/categories/get_categories'); $selectComponentData = array(0 => '-- '.lang('SELECT_COMPONENTS').' --'); $selectComponentJs = 'onChange="this.form.submit()" id="selectCatsByComponent"'; foreach($selectComponents as $option){ $selectComponentData[$option->id] = $option->name; } echo form_dropdown('selectCatsByComponent', $selectComponentData, 0, $selectComponentJs); echo form_close(); ?>
And output HTML is:
<form accept-charset="utf-8" method="post" action="http://127.0.0.1/backend/web/index.php/backend/categories/get_categories"> <select id="selectCatsByComponent" onchange="this.form.submit()" name="selectCatsByComponent"> <option selected="selected" value="0">-- Choose a component --</option> <option value="1">E-commerce</option> <option value="2">Content</option> </select> </form>
How to make it possible ?