Muhammad Salman
Muhammad Salman

Reputation: 19

Codeigniter php selected item from drop down

I need to populate the drop down from a directory for which I am using:

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

It works fine but how can I get the selected item from the menu? I have tried using:

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

But it does not work. Please help.Thank you.

Upvotes: 1

Views: 837

Answers (4)

noufalcep
noufalcep

Reputation: 3536

try this..

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

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

Upvotes: 1

Bond Zini
Bond Zini

Reputation: 1

This should work:

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

Basically, it creates an associative array before and passes it to the drop down. Hope it helps

Upvotes: 0

Romeo
Romeo

Reputation: 149

First get the value of the dropdown through jQuery

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

Then put it in a hidden text. To get the post value of it.

Upvotes: 2

DigitalDouble
DigitalDouble

Reputation: 1768

I'm not sure if this is gonna work because scandir() produces a numeric array while form_dropdown requires an associative array according to the documentation:

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

You might have to iterate through your $files array to convert it to an associative array and make sure that the keys are set to the proper values.

Upvotes: 0

Related Questions