簡體   English   中英

Codeigniter php選擇項目從下拉列表

[英]Codeigniter php selected item from drop down

我需要從我正在使用的目錄中填充下拉列表:

$dir = 'public/files/';
$files = scandir ($dir);
echo form_dropdown('myid', $files);

它工作正常,但我如何從菜單中獲取所選項目? 我嘗試過使用:

$selected=$this->input->post('myid');

但它不起作用。 請幫忙。謝謝。

首先通過jQuery獲取下拉列表的值

var selected = $('[name="myid"] option:selected')

然后把它放在一個隱藏的文本中。 獲取它的后期值。

嘗試這個..

$dir = 'public/files/';
$files = scandir ($dir);
$selected=$this->input->post('myid');

//add selected to the function
echo form_dropdown('myid', $files, $selected);

我不確定這是否會起作用,因為scandir()產生一個數字數組,form_dropdown需要一個根據文檔的關聯數組

$options = array(
    'small'  => 'Small Shirt',
    'med'    => 'Medium Shirt',
    'large'   => 'Large Shirt',
    'xlarge' => 'Extra Large Shirt',
);

您可能必須遍歷$files數組以將其轉換為關聯數組,並確保將鍵設置為正確的值。

這應該工作:

$dir = 'public/files/';
$files = scandir ($dir);
foreach($files as $file){
  $array_files[$file] = $file;
}
echo form_dropdown('myid', $array_files);  

基本上,它在創建關聯數組之前將其傳遞給下拉列表。 希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM