【问题标题】:select and display an image from a dropdown menu从下拉菜单中选择并显示图像
【发布时间】:2020-06-24 00:54:08
【问题描述】:

我有一个 php 代码,其中有一个下拉菜单,其中填充了特定文件夹中图像文件的名称,我想要的是一旦用户选择显示所选图像的名称之一,此时我正在尝试使用提交按钮。 我试图不使用 jquery 或其他语言

   <?php


$folder = 'C:\Users\source\\';

  echo '<form action="" method="post">'."\n".'<select name="image">'."\n".
 dropdown(image_filenames($folder), @$_POST['image']).
 '</select>'."\n".'<input type="submit" name="submit">'."\n".'
 </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>'."\n";
}
return $return;
}


if (isset($_POST['submit'])) {

echo '<img src=C:\Users\source'. $row['images'] . ' />';



}
   ?>

【问题讨论】:

  • 如果您希望图像在不重新加载页面的情况下显示出来,您将需要 javascript。
  • 您忘记在 img 标记中的 src 属性周围加上引号。除此之外,究竟是什么问题?您是否收到错误或代码根本没有执行您想要的?如果是这样,那么会发生什么
  • 一旦我按下提交按钮,它只会加载图像的图标而不是图像,我认为问题在于我使用的命名
  • $row它来自哪里...???
  • 在不使用表单的情况下拨打image_filenames($dir) 时,您是否获得了预期的结果?单出那个函数并尝试用它加载一个你知道很好的 ​​$dir ,你得到什么了吗? var_dump(image_filenames($dir));

标签: php html directory


【解决方案1】:

您必须将文件保存在项目中...

改变

$folder = 'C:\Users\source\\';

$folder = 'images\\';

images 是您项目中的文件夹名称...

xampp 或 wampp 不考虑本地路径文件...

而且它也不会显示图像...

希望对您有所帮助...

我在您的代码中更改了 两个地方...

 <?php


    $folder = 'images\\'; // changed here

      echo '<form action="" method="post">'."\n".'<select name="image">'."\n".
     dropdown(image_filenames($folder), @$_POST['image']).
     '</select>'."\n".'<input type="submit" name="submit">'."\n".'
     </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>'."\n";
    }
    return $return;
    }


    if (isset($_POST['submit'])) {

    echo '<img src=images\\'. $_POST['image'] . ' />';  // changed here



    }
       ?>

【讨论】:

  • 刚刚试了下,读取文件路径正常,但还是加载不出来
  • 这很好用,我只是将所有图像放在程序所在的文件夹中,它正在运行,谢谢
猜你喜欢
  • 2020-09-11
  • 2010-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-20
  • 2020-08-01
  • 2011-06-16
相关资源
最近更新 更多