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?

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

How to repopulate form data along with error after submitting form

Simply add $_POST['fieldName'] as an input value

<form action="code.php" method="POST" enctype="multipart/form-data">


<hr style="border-top: 3px solid #ccc; background: transparent;">

<h5>Add Documents</h5>
<hr style="border-top: 1px solid #ccc; background: transparent;">

<div class="form-group">
<label>Title</label>
<input type="text" name="doc_title" class="form-control" value="<?php echo isset($_POST['doc_title']) ? $_POST['doc_title'] : '' ?>" required>
</div>

<div class="form-group">
<label>Documents </label>
<input type="file" name="doc" class="form-control" id="image" required>
</div>

<div class="float-right mb-2">
<button type="submit" name="adddoc" class="btn btn-primary">Save</button>
<a href="doc.php" class="btn btn-danger">Cancel</a>
</div>
</form>

Restoring the value of a input type=file after failed validation

You cannot populate the file input, to do so would be a serious security problem.

Instead, store the uploaded content on the server and store an id that you can use to reference it in a hidden input.

Clean the files up automatically after they reach a certain age, and provide a means for the user to change their mind about what file they want to upload (e.g. a checkbox (checked by default) for each file being stored on the server for upload)

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

Form::file: How to repopulate with Input::old After Validation Errors and/or on Update?

Apparently you can't.
Browsers don't allow you to set a value on a file input for security reasons.

There are two silimar questions, not laravel related but i think answers could help you:

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

Restoring the value of a input type=file after failed validation

How to retain file input value after form submission with errors?

i think you already suggested the answer to your problem . it owuld be better to validate other fields of the form using ajax and don't submit the file initially. anbd if you have some problems with the file you can have some basic checks for your file using like file extension using ajax

Prevent HTML file field from being delete after PHP validation roundtrip

This is not possible - you have to store the file on the server to do this. Then you can show the user a short info about the already stored file and the option to upload a new file or delete the file



Related Topics



Leave a reply



Submit