【问题标题】:Dynamically created input type=file not working in PHP? [duplicate]动态创建的输入类型=文件在 PHP 中不起作用? [复制]
【发布时间】:2014-05-02 19:20:00
【问题描述】:

我有一个正在测试的表单,我希望用户能够根据需要上传尽可能多的文件。

这是表格,我设置了 enctype。表单不会自动关闭 /> 。如果输入字段不是即时生成的,服务器会接受来自其他脚本甚至来自该脚本的上传文件。

<table class="add-table">
    <tr>
        <td colspan="3">
            Input New Specification Details
            <form method="post"     action="specifications.php">
                <input type="submit" value="Go     Back" />
            </form>
        </td>
    </tr>
    <form action="specifications-add.php" method="post" enctype="multipart/form-data">
        <tr>
            <td><label>Specification Name:</label></td>
            <td><input type="text" name="spec-name" value="<?php echo $spec_name; ?>" /></td>
            <td rowspan="3" class="add-spec-save-cell"><input type="submit" name="save-new-spec" value="Save Specification" /></td>
        </tr>
        <tr>
            <td><label>Specification Type:</label></td>
            <td>
                <input type="radio" name="spec-type" value="astm" />ASTM<br />
                <input type="radio" name="spec-type" value="oem"  />OEM<br />
                <input type="radio" name="spec-type" value="none"  />None
            </td>
        </tr>
        <tr>
            <td><label>Attachments:</label></td>
            <td><button id="add-another-row">+ Add Row</button></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td colspan="2" id="attachments-cell">
                <!-- Dynamic code appended here -->
            </td>
        </tr>
    </form>
</table>

这是生成表单的 javascript (jQuery):

$("button#add-another-row").click(function () {
    var newRow = "<div class='new-attachment-row'>";
    newRow += "<input name='attachments[]' type='file' />";
    newRow += "<button class='delete-btn'>Delete </button></div>";
    $("td#attachments-cell").append(newRow);

    return false;
});

如果我只是在表单中添加一个简单的&lt;input name="attachment" type="file"&gt;,$_FILES 数组就会被填充。如果我使用 javascript 动态生成脚本,则 $_FILES 数据不会被填充,并且在我调用 var_dump($_FILES); 时不会显示任何值;

这是一个已知的限制或错误吗?我不知道我做错了什么。

视觉供您参考:

【问题讨论】:

  • 如果我理解你想要做什么,你肯定可以做到,所以我认为你的代码中一定有一些东西在创建表单的文件上传部分?你能发布 JavaScript 吗?
  • 我同意 Andrew,这是完全可行的,php 是服务器端的,不在乎添加了哪个输入。你应该彻底检查你的javascript
  • 我看到了可能的重复,答案说它是一个未封闭的表格。我看到我的表单已正确关闭。
  • 我已经删除了所有 PHP 代码,所以名称应该无关紧要。 PHP 唯一在做的就是 var_dump($_FILES);使用动态时输出:array(0) { },使用静态时输出:array(1) { ["attachments"]=> array(5) { ["name"]=> string(9) "test1 .txt" ["type"]=> string(10) "text/plain" ["tmp_name"]=> string(25) "C:\WINDOWS\Temp\php22.tmp" ["error"]=> int (0) ["size"]=> int(0) } }
  • 你不能在&lt;tr&gt;周围加上&lt;form&gt;

标签: javascript php forms


【解决方案1】:

您的 HTML 无效。

表单元素不能是表格的子元素。浏览器将尝试挽救文档。在您的控制台中查看该页面,您将看到它如何改变您的文档。

您需要更改您的代码以使其有效。使用表格进行布局是个坏主意。

【讨论】:

  • 是的,这最终是正确的答案。尽管如此,用于布局的表格在所有浏览器中都非常好且可预测,并且非常适合表单。它们为原本无法预测的浮动列和混乱的 CSS 提供了很好的结构。我知道语义表不是用于布局,但实际上它们是最好的,因为它们是结构化的并且易于理解/可编辑,尤其是对于表单。我希望我们能得到一个 标签,而不是嵌套的 div 或非语义的 UL、LI 布局“解决方案”。
  • 我不明白为什么我的无效 HTML 在输入字段为静态时有效,以及为什么它在动态时中断。在这两种情况下,&lt;form&gt;&lt;tr&gt; 中的输入字段的位置都不会改变。
猜你喜欢
  • 2020-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-28
  • 1970-01-01
  • 1970-01-01
  • 2019-07-17
相关资源
最近更新 更多