Can you re-populate file inputs after failed form submission with PHP or JavaScript? Can you re-populate file inputs after failed form submission with PHP or JavaScript? php php

Can you re-populate file inputs after failed form submission with PHP or JavaScript?


I think the short answer here is no. You can't repopulate file upload fields. However, you can work around it.

If a file has been selected and the form submitted, then you've already received the file. What you can do is keep a reference to the file on disk and put that in a hidden field and show a message to indicate to the user you still have their file uploaded so it does not need to be replaced/re-uploaded. When your form gets submitted again without a file, you can check for the hidden field value and use that to get your local copy of the file they uploaded in their last attempt.

The other way to do this is to either submit the form via ajax (using either flash or the iframe method for the file upload) or to do an ajax call to validate the form first, and then only submit as normal if valid (thus no form reload, no loss of file upload field data).


I solved this problem by let user pick upload file as the last field before submit the form ^^