2013-05-11 14 views
1

我將如何訪問此網址index.php/backend/categories/get_categories/$id<select>下拉onChange?下拉選擇onChange重定向到CodeIgniter中的參數與網址

我的意思是,如果我要去index.php/backend/categories/get_categories/1,它會輸出一個表格,其中包含component id 1之下的所有類別。

我想要的是,一旦我從下拉列表中選擇一個選項,我希望它提交併重定向到相同的URL,但具有<option val="$id">的特定ID?

要生成使用CodeIgniter表單輔助下拉列表:

<?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(); 
?> 

輸出HTML是:

<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> 

如何可能?

回答

1

如果我沒有弄錯你,你在問這樣的事情;

在控制器中;

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

現在你可以在視圖訪問的id值這樣

if(isset($id)) // if there is a $id loaded 
{ 
    // Your view accoring to $id 
} 

注意:當我在寫這個代碼,我沒有一個測試環境。所以代碼不是complate,可能有一些錯誤

+0

它的作品男人,非常感謝你! – aspirinemaga 2013-05-11 15:12:57

0
You can use ajax with jquery onchange function, see the below example: 
$("#bussiness").live('change',function(){ 
    var bus_id=$(this).val(); 
    $.ajax({ 
     url : "<?php echo base_url(); ?>setting/set_session/"+bus_id, 
     success:function(res) { 
      document.getElementById('output').HTML = res; 
     }   
    }); 

}); 
+0

我不想在這裏使用Javascript。我需要一個清晰的PHP代碼。但無論如何感謝您的建議 – aspirinemaga 2013-05-11 08:16:56