在用户提交时使用选择框获取图像

时间:2014-12-25 13:01:17

标签: php forms user-input

大家好我需要一些关于php的典型信息

我正在创建一个有多个选项的选择框。我有6张图片,然后我需要当用户选择1,2,3并提交然后image1,image2,image3必须在页面上显示,当用户选择2,4,6并提交然后image2,image4,image6必须显示我在PHP中做什么来获得这个代码我trid很多buut没有运气帮助我谢谢。

   <form method="get">
        <select name="images" multiple="multiple">
            <option name="img" value="image">1</option>
            <option name="img" value="image">2</option>
            <option name="img" value="image">3</option>
            <option name="img" value="image">4</option>
            <option name="img" value="image">5</option>
            <option name="img" value="image">6</option>
        </select>
        <input type="submit" value="submit" name="submit">
    </form>

这是php

<?php
//path to directory to scan. i have included a wildcard for a subdirectory
$directory = "images/*/";

//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");

$imgs = '';
// create array
foreach($images as $image){ $imgs[] = "$image"; }

//shuffle array
shuffle($imgs);

//select first 20 images in randomized array
$imgs = array_slice($imgs, 0, 20);

//display images
foreach ($imgs as $img) {
echo "<img src='$img' /> ";
}
?>

但我希望这项工作能够提交感谢

4 个答案:

答案 0 :(得分:1)

你应该通过命名select field =&#34; images []&#34;来引用用户选择。 EXP:

<select name="images[]" multiple="multiple">
<option value="1">1</option>
..
</select>

然后在php中,您可以访问提交的值$ _GET [&#39; images&#39;]。它按顺序为您提供了一系列提交的值

答案 1 :(得分:0)

用这个&#39;包裹您的PHP代码。语句:

if (isset($_GET['submit']))
{
    // Your code
}

答案 2 :(得分:0)

我发现了2个错误,你可以从那里继续: 1.在表单中,您必须在表单中包含一些操作 - 在提交后将打开的文件的名称,并将通过$ _GET接收数据。 2.你需要制作一个foreach - endforeach循环并打印图片的数据,而不是重复选项行......

看起来应该是这样的:

<?php
//path to directory to scan. i have included a wildcard for a subdirectory
$directory = "imagesend/*/";

//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");

$imgs = '';
// create array
foreach($images as $image){ $imgs[] = "$image"; }

//shuffle array
shuffle($imgs);

//select first 20 images in randomized array
$imgs = array_slice($imgs, 0, 20);


?>
<form method="get" action="XXX.php">
<select name="images" multiple="multiple">
<?php foreach ($imgs as $img):  ?>

<option name="<?php echo $img; ?>" value="<?php echo $img; ?>"><img src="<?php echo $img; ?>"/></option>

<?php endforeach; ?>
</select>
<input type="submit" value="submit" name="submit">
</form>

答案 3 :(得分:0)

工作代码

<html>
 <head>
 <title>Listing 9.15 A file upload script</title>
 </head>
 <?php
if ( isset( $_POST['img']) )
 {
    $im="images/".$_POST['img'].".jpg";
    echo "".$im;
    echo "<img src='".$im ."'/>";
 }
 else
 {
     print "Error";
 }

 ?>
 <body>
 <form enctype="multipart/form-data" action="<?php print $_SERVER['PHP_SELF'] ?>" method="POST">
        <select name="img" multiple="multiple">
            <option  value="image1">1</option>
            <option  value="image2">2</option>
            <option  value="image3">3</option>
            <option  value="image4">4</option>
            <option  value="image5">5</option>
            <option  value="image6">6</option>
        </select>
        <input type="submit" value="submit" name="submit">
    </form>
 </body>
 </html>