Jump to content

Get value from drop down list


IamTomCat

Recommended Posts

Hi ,

 

I am pretty new to php. I would like to create a php website based on folders. 

 

I have a code so far that shows the names of folder in a selection box. What I would like to achieve now is how i get the images inside the folder after I click on submit. I know how to get the images with a glob function. What I don't know is how to get the value of each specific folder name. How would I do that? 

 

I have this code so far to get the names of the folders in the selection box:

<form action="test.php" method="post">
        <select name="myDirs"> 
            <option value="" selected="selected">Select a genre</option>
            <?php
            $dirs = glob("*", GLOB_ONLYDIR);
            foreach($dirs as $val){
                echo '<option value="'.$val.'">'.$val."</option>\n";
            }
            ?>
</select>
<input type="submit" name="submit2">
<?php 

if (isset($_POST['submit2']))
foreach($dirs as $val){
        
{
echo $val;
}
}     
        ?>
        </form>

 

How could get I'm now images which are in the folder of which folder names appear in the selection box?

 

 

Link to comment
Share on other sites

Hi. Many thanks for the reply. Unfortunat it didn't work. My main issues how I access the value of each folder. And then for each folder to access the images. Let's say I select the folder name "Toy story". Then I want all the images that are in the folder Toy Story. When I select the let's say the folder name "Brain", then I want every image that is in the folder "Brain". Help would be very much appreciated. 

Link to comment
Share on other sites

I just tested it and this worked fine. You'd just need to define the $source_dir at the top.

<?php
  $source_dir = '/home/users/images/';
?>
<form action="test.php" method="post">
<select name="myDirs"> 
  <option value="" selected="selected">Select a genre</option>
  <?php
    foreach(glob($source_dir . "*", GLOB_ONLYDIR) as $val){
      echo '<option value="'.$val.'">'.$val."</option>\n";
    }
  ?>
</select>
<input type="submit" name="submit2">
</form>
<?php 
if (isset($_POST['submit2']))
{
  //get the dir from POST
  $selected_dir = $_POST['myDirs'];
  //now get the files from within the selected dir and echo them to the screen
  foreach(glob($selected_dir . DIRECTORY_SEPARATOR . '*') as $filename)
  {
    echo "$filename<br>";
  }
}
?>
Edited by CroNiX
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.