1

I have a form that I'm testing that I want the user to be able to upload as many files as they need.

Here's the form, I have the enctype set. The form is not auto closed /> . The server accepts uploaded files from other scripts and even from this script if the input fields are not generated on the fly.

<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>

Here is the javascript (jQuery) that generates the form:

$("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;
});

If I just add a simple <input name="attachment" type="file"> to the the form, the $_FILES array gets populated. If I use the javascript to dynamically generate the script the $_FILES data is not populated and does not show any values when I call var_dump($_FILES);

Is this a known limitation or bug? I can't figure out what I'm doing wrong.

visual for your reference: enter image description here

9
  • If I understand what you are trying to do, you definitely can do it, so I think there must be something off in your code that is creating the file upload part of the form? Can you post the JavaScript? Commented May 2, 2014 at 19:23
  • I agree with Andrew, this is totally doable, php is server side and doesn't care what added which input. You should thouroughly check your javascript Commented May 2, 2014 at 19:24
  • I saw that possible duplicate and the answer to that said it was an unclosed form. I see that my forms are closed properly. Commented May 2, 2014 at 19:27
  • I've stripped out all PHP code so the name should not matter. The only thing the PHP is doing is var_dump($_FILES); which outputs this: array(0) { } when using dynamic and when using static it outputs: 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) } } Commented May 2, 2014 at 19:33
  • 1
    You can't put a <form> around <tr>. Commented May 2, 2014 at 19:36

1 Answer 1

2

Your HTML is NOT valid.

A form element can not be a child of a table. The browser will try to salvage the document. View the page in your console and you will see how it alters your document.

You need to change your code so it is valid. Using a table for layout is a bad idea.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes this ended up being the correct answer. Although, tables for layouts are very nice and predictable across all browsers and lend themselves very nicely to forms. They give nice structure to otherwise unpredicatble floating columns and messy CSS. I know semantically tables are not for layout but practically they are the best thing because they are structured and easily understandable/editable especially for forms. I wish we could get a <tablelayout> tag instead of nested div or the un-semantic UL, LI layout "solutions".
What I don't get is why my invalid HTML worked when the input field was static and why it broke when it was dynamic. The position of the input field in the <form> in the <tr> would not have been altered in either case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.