如何为有值的输入元素传递HTML ID

How to pass HTML ID for input elements that have values

本文关键字:元素 HTML ID 输入      更新时间:2023-09-26

我有以下表单,供人们更新数据库中的图像(通过更新图像,基本上只是上传新图像,并更新图像路径)

<input type="file" name="color" id="1" />
<input type="file" name="color" id="2" />
<input type="file" name="color" id="3" />
...
<a onclick="submitFunc();" ></a>

我如何在php中获得submitFunc()的id值,以便我知道人们想要更新哪个图像?

如果($id==2)不为空,我知道人们想要更新图像#2等,但我如何传递该ID值?

也有可能一次更新多个文件。

为名称添加一个数组,使用id作为键:

<input type="file" name="color[1]"  />
<input type="file" name="color[2]"  />
<input type="file" name="color[3]"  />

然后在你收到的PHP检查

if(is_array($_FILES['color'])):
  foreach ($_FILES['color'] as $id=>$file):
    if ($file['size']):
      // upload if changed. $id is your image id