使用 PHP 或 JavaScript 提交表单失败后,您可以重新填充文件输入吗? [英] Can you re-populate file inputs after failed form submission with PHP or JavaScript?

查看:12
本文介绍了使用 PHP 或 JavaScript 提交表单失败后,您可以重新填充文件输入吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这里是场景:

用户将看到包含文件输入的表单.用户提交表单.出于某种原因,表单未通过验证/验证.向用户显示错误突出显示的表单.文件输入现在为空白.

User is presented with form that contains file inputs. User submits form. Form does not pass validation/verification for one reason or another. User is presented form with errors highlighted. File inputs are now blank.

问题:

是否可以使用用户最初选择的文件的路径重新填充文件输入.PHP 解决方案是理想的,但我也愿意接受 JavaScript 解决方案.

Is it possible to re-populate the file inputs with the paths to the files the user originally selected. A PHP solution would be ideal, but I'm also open to JavaScript solutions as well.

推荐答案

我认为这里的简短回答是否定的.您无法重新填充文件上传字段.不过,您可以解决这个问题.

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.

另一种方法是通过ajax提交表单(使用flash或iframe方法上传文件)或先调用ajax来验证表单,如果有效,则只提交正常(因此无需重新加载表单,也不会丢失文件上传字段数据).

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

这篇关于使用 PHP 或 JavaScript 提交表单失败后,您可以重新填充文件输入吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆