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

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

问题描述

好吧,这是这样的情况:

用户呈现包含文件输入的表单。
用户提交表单。
表单由于某种原因没有通过验证/验证。
用户呈现形式,突出显示错误。
文件输入现在空白。

问题:

是否可以重新填充文件输入与用户最初选择的文件的路径。一个PHP解决方案将是理想的,但我也向JavaScript解决方案开放。

解决方案

我想这里的简短答案没有。您不能重新填充文件上传字段。然而,你可以解决它。



如果一个文件已被选中,表单提交,那么你已经收到了文件。你可以做的是保持对磁盘上的文件的引用,并把它放在一个隐藏的字段,并显示一条消息,以指示用户你还有他们的文件上传,所以它不需要被替换/重新上传。如果您的表单在没有文件的情况下再次提交,您可以检查隐藏的字段值,并使用它获取上次尝试上传的文件的本地副本。



另一种方法是通过ajax提交表单(使用flash或iframe方法进行文件上传),或者先执行ajax调用来验证表单,然后只有在正常情况下才提交(如果有效的话)没有表单重新加载,不丢失文件上传字段数据)。

Okay so here's the scenario:

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.

Question:

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.

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天全站免登陆