Todd Williams
Todd Williams

Reputation: 267

How to filter select options by file type with PHP

I have a folder on the server with several different file types, and the only ones I want to appear in the <select> list as options are the .json files. Currently all the files are displaying as <options>. I've only seen solutions for <input> elements, but not for a <select>. How can I display only the .json files in the folder? Here is my element:

<form action="/server/php/data/process.php" method="post">
     <select type="text" name="selectTemplate" onchange="loadTemplate()">
           <option value="" selected="selected">Select Template</option>
                    <?php
                         foreach(glob(dirname(__FILE__) . '/server/php/data/*') as $filename){
                         $filename = basename($filename);
                         echo "<option value='" . $filename . "'>".$filename."</option>";
                      }
                    ?>
      </select>
</form>

Upvotes: 0

Views: 168

Answers (1)

Sebastian Breit
Sebastian Breit

Reputation: 6159

Change this line:

foreach(glob(dirname(__FILE__) . '/server/php/data/*') as $filename){

like this:

foreach(glob(dirname(__FILE__) . '/server/php/data/*.json') as $filename){

Upvotes: 1

Related Questions