1

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.

1
  • Can you use Inspect Element on the generated dropdown and verify if they have values attribute set? Commented May 11, 2015 at 5:29

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

try this..

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

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

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.