PHP提交带有图像的表单.访问下一页上的图像


PHP Submitting form with images. Access that image on the next page

我在尝试从下一页的表单访问提交的图像时遇到问题。

这是我的html中的php。

我正在循环浏览一个图像文件夹,并将它们回显到页面上。

<form action = "Order_Form.php"
    method = "post"
    enctype = "multipart/form-data">
<?php
    $files = glob("images/*.*");
    $count = 0;

for ($i=0; $i<count($files); $i++)
{ 
 $num = $files[$i]; 
    if($count == 4){
      echo "<br>";
      $count = 0;
    }
    $count++;

    echo '<input type="image" src="'.$num.'" 
    alt="img"  name="image" class = "galImgs" value="submit"  />';
}

?>
</form>

现在提交后,我进入Order_Form.php

我想显示用户点击/提交到页面的图片。我一直在尝试

      $nm = $_POST['image'];      
      echo '<img src="/files/images/.$nm" class= "image"/>';

我很确定它必须是一些简单的东西,但经过大量的谷歌搜索和试验&错误我好像想不通。

如有任何帮助,我们将不胜感激。非常感谢。

按钮的值将是其value属性的值,或者始终是Submit

通过将[$num]附加到输入的name属性,使用$_POST中的图像阵列。

echo '<input type="image" src="'.$num.'" 
    alt="img"  name="image[' . $num . ']" class = "galImgs" value="submit"  />';

这将把输入image作为一个看起来像的数组发送到$_POST

// Suppose two of them (22 & 14) were clicked (which isn't actually possible but illustrates it)
// The values are always "submit" but the keys are useful to you.
array(1) {
  ["image"]=>
  array(2) {
    [22]=>
    string(6) "submit"
    [14]=>
    string(6) "submit"
  }
}

然后,当你从$_POST中检索它时,你知道哪个被点击了:

foreach ($_POST['image'] as $im=>$num) {
   // The array key holds the image number
  echo '<img src="/files/images/' .$im .'" class= "image"/>';
}

您的$nm变量没有被解释,因为它位于单引号中。

如果您简单地将普通的<img>标记与onClick事件一起使用,将隐藏的输入字段设置为所选图像的值,您可能会更轻松。