$_FILES array is empty in php with multiple forms on one html page

I have an html page with multiple forms on it.
I created an example here: http://jsfiddle.net/nteuscher/ht7334p6/

<table border="2">
 <tr>
  <form action="" method="post">
  <td colspan="7" align="left"><input type="hidden" 
    name="action" value="Edit">
   <input type="submit" value="Edit Profile">
  </td>
  </form>
 </tr>
 <tr>
   <form action="" method="post" enctype="multipart/form-data">
   <td> 
    <div class="upload">
     <label for="upload">File: <input type="file" 
       id="upload" name="upload"></label>
    </div>
    <div class="upload">
      <input type="hidden" name="uploadaction" value="ypt">
      <input type="submit" value="Upload">
    </div>
  </td>
 </form>
 </tr>
</table>

When I have 2 different forms on the page, the $_FILES variable is empty when someone clicks the “Upload” button. If have only the file upload form on the page, the $_FILES variable works as expected and contains all of the necessary information. I verified this by doing a var_dump($_FILES) using my php controller file.

I have checked the php.ini file to make sure that uploads are working, and I as noted, if I remove all forms except the one for file uploads, then everything works as expected.

Any suggestions on how to manage this? I could create a separate page, but I would rather allow uploads directly from this one page.

I would say as your example only has the one form which is working so it is not a lot of help locating the problem.
I would have an example of what is not working.

I assume all your inputs have different names?

I would probably have one form with different sections and one submit button.

Thanks Rubble. All items have different names. I can’t use a single submit button because there are several different items on a single page. I simplified the problem here for illustration and so I don’t have to post all of my code. I will keep looking for a solution.

Maybe not.

I see 2
<input type="submit"
neither of which has a distinguishing name

What does the $_POST variable show?

The $_POST variable shows the value of “Upload” along with the name of the file selected for upload. So the form is submitting the correct data to $_POST, but the $_FILES variable is getting cleared. I need to look at my controller script and see if I made a mistake in the processing that unintentially clears $_FILES. (BTW, how do I get the underscore to stay? It keeps converting my text to italics)

the easiest way is to escape them or enclose them in backticks
$\_POST
$_POST
`$_POST`
$_POST

I agree that having a “multi-form-in-one” is an unusual way to go about it, but that in itself may not be the problem.

Maybe all the form tags need to have the
enctype="multipart/form-data"

Thanks for the tip on the formatting!

I found the error … it was bad html … I had <form> tag that didn’t have its </form> closing tag. That ran into my new form … now it is working! Thank you for the help!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.