如何在HTML中还原表单元素的内容 [英] How to restore the contents of a form elements in HTML

查看:144
本文介绍了如何在HTML中还原表单元素的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有一个简单的HTML表单.当我提交此表单时,如果数据无效,则会出现错误,并且将擦除表单的所有内容.我不想删除html表单的内容,这样用户就不必再次填写所有表单.

我该怎么做?

I have a simple Html form in my web. When I submit this form and if the data is not valid then it gives the error and all the contents of the form are wiped out. I do not want to wipe out the contents of the html form, so that user do not have to fill all the form again.

How can I do that?

推荐答案

确定.这是我的方法.

我没有使用PHP/HTML页面,而是创建了PHP代码,该代码使用某些基于XML的模板生成整个页面.该模板具有以下参数:表单字段中的填充数据.如果什么也没有张贴,则页面将生成而没有填充数据-所有参数都将获得默认值,其中大多数是空字符串. POST完成后,将运行相同的PHP脚本,这次包含POST数据.此数据用于页面生成的PHP函数中的参数.将还原所有已填写的数据,并计算并添加其他数据:例如,数据验证信息,例如错误和关于在下一次POST中应修复的问题的建议.

—SA
Sure. Here is how I did it.

Instead of having a PHP/HTML page I created PHP code which generates the whole page out of some XML-based template. This template has parameters: the filled-in data in the fields of the forms. When nothing is posted the page is generated without filled-in data — all parameters gets default values, most of them are empty strings. When the POST is done, the same PHP script runs, this time with POST data. This data is used for parameters in the page-generated PHP function. All filled-in data is restored, plus additional data is calculated and added: for example, data validation information, such as errors and recommendation on what should be fixed be the user on next POST.

—SA


我将通过一个示例来演示您的问题的解决方案.您需要做的是为每个表单输入添加一个值.该输入字段应进行PHP参数化,以确保回发后不会丢失数据.例如:

I will demonstrate the solution of your problem with an example. What you need to do is add a value to each of your form inputs. This input field should be PHP parameterized to ensure that on post-back, the data is not lost. For example:

<?php
if(isset(


_POST [' form1'])) ){ if(!empty(
_POST['form1'])){ if(!empty(


这篇关于如何在HTML中还原表单元素的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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