将HTML表单模板存储在数据库中 [英] Storing HTML form templates in a database

查看:377
本文介绍了将HTML表单模板存储在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我允许用户使用任意数量的输入和选择的元素来创建自定义表单.然后,我希望用户能够将该表单另存为各种模板,以便其他用户可以使用该模板来创建自己的自定义表单.

I am allowing users to create custom forms with any number of inputs and elements of their choosing. I then want the user to be able to save that form as a template of sorts so that other users could use that template to create their own custom form just like it.

可能有数百种不同的帖子类型,每种都有不同的元素,等等.

There could be hundreds of different post types, each one with different elements etc.

在PHP和MySQL中完成此操作的最佳方法是什么?

What might be the best way to accomplish this in PHP and MySQL?

我正在考虑,一旦用户在数据库中创建了自定义表单的html,就将其存储起来.因此,如果另一个用户想要使用该模板,我只需从数据库中检索该html并将其呈现给新用户即可.

I am thinking of just storing the html of the custom form once a user has created it in the database. And so if another user wants to use that template, I'd just retrieve that html from the database and present it to the new user.

这是最好的方法吗?许多网站都做类似的事情(例如,Wufoo),对我来说,将这样的原始html存储在数据库中似乎有点怪异.

Is this the best way to do this? Many sites do similar things (Wufoo, for example), it just seems a little weird to me to store raw html like that in a database.

推荐答案

每当您考虑将HTML存储在数据库中时,您可能都在错误的行上.

Anytime you're thinking of storing HTML in a database you're probably on the wrong lines.

如果要对所有形式进行广义的结构更改怎么办?您突然必须浏览所有记录并进行更改.

What if you want to make generalised structural changes to all forms? You'd suddenly have to go through all records and make that change.

相反,请考虑仅使用数据库来存储稍后重新创建表单所需的基本内容.

Instead, think in terms of using the DB only to store the bare bones of what's necessary to recreate the form later.

也许您将有一个数据库表来存储表单,另一个表与它具有一对多的关系来存储每个字段定义,依此类推.

Perhaps you'd have one DB table to store forms, another table with a one-to-many relationship to it that stores each field definition, and so on.

因此,您的数据库将存储输入的数量,它们的类型,标签,验证要求-随便什么.然后,您将拥有一个庞大的脚本,可以浏览这些脚本,然后-瞧-重建表单.

So your DB would store the number of inputs, their type, labels, validation requirements - whatever. Then you'd have a big script that would rifle through these and - voila - rebuild the form.

这种方法:

  • 使您可以更好地控制常规HTML结构
  • 更紧密地遵循规范化数据库数据的概念
  • 坚持将数据和结构(即HTML)分开的概念
  • 减少存储的数据量(没有重复的标签,空格等)

这篇关于将HTML表单模板存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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