Harugawa February 2016

How to extract value from an select option form which happens to be a name of directory

Honestly, I've got not a sinle idea.

            <?php
                $somePath = 'randomfoldername';
                $dir = new DirectoryIterator($somePath);
                foreach ($dir as $fileinfo) {
                    if ($fileinfo->isDir() && !$fileinfo->isDot()) {
                        echo '<option value="$fileinfo->getFilename[]">'.$fileinfo->getFilename().'</option>';
                    }
                }
        ?>

Tried to echo it and got that

$fileinfo->getFilename

in result.

Probably doing something terribly wrong. Not even sure if you know what I mean. I just want to use the value after it is being posted but can't make it work.

Answers


Franz Gleichmann February 2016

<?php
  $somePath = 'randomfoldername';
  $dir = new DirectoryIterator($somePath);
  foreach ($dir as $fileinfo) {
    if ($fileinfo->isDir() && !$fileinfo->isDot()) {
      echo '<option value="'.$fileinfo->getFilename().'">'.$fileinfo->getFilename().'</option>';
    }
  }
?>


Sergey Gurevich February 2016

You need to write the value you want as the value property of the <option> tag.

Try this:

            <?php
            $somePath = 'randomfoldername';
            $dir = new DirectoryIterator($somePath);
            foreach ($dir as $fileinfo) {
                if ($fileinfo->isDir() && !$fileinfo->isDot()) {
                    $fileName = $fileinfo->getFilename();
                    echo '<option value="'.$fileName.'">'.$fileName.'</option>';
                }
            }
        ?>

Another thing is that you should probably avoid inline code. If you intend on using pure PHP you should reduce the <option>...</option> output into a variable and insert this variable alone into your HTML.

            <?php
            $somePath = 'randomfoldername';
            $dir = new DirectoryIterator($somePath);
            $options = '';
            foreach ($dir as $fileinfo) {
                if ($fileinfo->isDir() && !$fileinfo->isDot()) {
                    $fileName = $fileinfo->getFilename();
                    $options .= '<option value="'.$fileName.'">'.$fileName.'</option>';
                }
            }
        ?>


        ...

        <select><?php echo $options ?></select>

Post Status

Asked in February 2016
Viewed 2,599 times
Voted 10
Answered 2 times

Search




Leave an answer