douping7975 2015-12-07 02:54
浏览 40

从php下拉菜单中显示所选图像

So I have this working code:

<?php 
                $folder = './images/'; 

                echo '<form action="" method="post">'."
".'<select name="image">'."
". 
                     dropdown(image_filenames($folder), @$_POST['image']). 
                     '</select>'."
".'</form>'; 
                function image_filenames($dir) 
                { 
                    $handle = @opendir($dir) 
                        or die("I cannot open the directory '<b>$dir</b>' for reading."); 
                    $images = array(); 
                    while (false !== ($file = readdir($handle))) 
                    { 
                        if (eregi('\.(jpg|gif|png)$', $file)) 
                        { 
                            $images[] = $file; 
                        } 
                    } 
                    closedir($handle); 
                    return $images; 
                }   
                function dropdown($options_array, $selected = null) 
                { 
                    $return = null; 
                    foreach($options_array as $option) 
                    { 
                        $return .= '<option value="'.$option.'"'. 
                                   (($option == $selected) ? ' selected="selected"' : null ). 
                                   '>'.$option.'</option>'."
"; 
                    } 
                    return $return; 
                } 
                ?>

This is creating a drop down menu with the contents listed from my /images folder. How to I then post the selected image?

  • 写回答

1条回答 默认 最新

  • dtjo87679 2015-12-07 07:38
    关注

    If you add a submit button to the form you can then post the selected image. In the below code it checks for the post variable (if the form is submitted), and you can then do something with $_POST['image'] which is the value selected in the form.

    // if the form is submitted
    if (isset($_POST['submit'])) {
        // the selected image will be $_POST['image']
            // do something with the posted image name
        var_dump($_POST);
    }
    
    $folder = './images/'; 
    
    echo '<form action="" method="post">'."
    ".'<select name="image">'."
    ". 
         dropdown(image_filenames($folder), @$_POST['image']). 
         '</select>'."
    ".'<input type="submit" name="submit">'."
    ".'
         </form>'; 
    function image_filenames($dir) 
    { 
        $handle = @opendir($dir) 
            or die("I cannot open the directory '<b>$dir</b>' for reading."); 
        $images = array(); 
        while (false !== ($file = readdir($handle))) 
        { 
            if (preg_match('/^.*\.(jpg|jpeg|png|gif|svg)$/', $file)) 
            { 
                $images[] = $file; 
            } 
        } 
        closedir($handle); 
        return $images; 
    }   
    function dropdown($options_array, $selected = null) 
    { 
        $return = null; 
        foreach($options_array as $option) 
        { 
            $return .= '<option value="'.$option.'"'. 
                       (($option == $selected) ? ' selected="selected"' : null ). 
                       '>'.$option.'</option>'."
    "; 
        } 
        return $return; 
    } 
    
    评论

报告相同问题?