0

I have a situation where I have several ways to perform the same activity in my php web app.

There is the "Manage Widgets" section of the app which has a form for creating new widgets and a list of existing widgets.

Then somewhere else in the app there is a button that pops up a dialog to add a new widget.

Then on the home page of the app there is another place where a form is embedded to add a widget (think home page portal).

My question is: What is the best practice for this? In this case all of the forms will be essentially the same. So my first instinct is to use the same code for all three of these scenarios. On the other hand, space on the home page could be smaller and layouts may have to differ between the three.

So even though it would be repetition, is it better to duplicate this form 3 times (there is a proper model layer, so the duplicated code would not include the logic to add/edit the widget)? Or try to force a single view in all of these scenarios? Both seem wrong to me and I am hoping for some ideas to discover some sort of middle ground.

4

3 回答 3

1

一种方法是将表单的标记(而不是样式)作为独立文件,然后可以从您喜欢的任何地方包含该文件。

然后,您可以使用 AJAX 将表单提交到特定的 PHP 脚本,该脚本处理表单提交并返回有意义的 JSON 响应。然后,您可以在相关页面上显示此 JSON 响应并为其设置样式。

这样,您就拥有了一个表单(可以采用不同的样式)和一个处理程序,用于使用该表单所需的任何视图。

于 2013-05-16T11:02:47.220 回答
0

我的意思是,最好的方法是从其他表单中组合表单(不要重复自己)。您可以对同一表单使用不同的模板来更改最终表单的外观。

例如/idea,您可以检查 Nette Framework 中使用的表单(http://doc.nette.org/en/forms

T。

于 2013-05-13T21:36:16.230 回答
0

如果您只是更改样式而不是标记,我认为最好的方法是向表单元素添加一个特定的类,然后使用 Javascript(不是 Ajax,justa Javascript)根据需要在这些类之间交替。

如果您的应用程序根本不使用 Ajax,而您只是使用 PHP 生成网页,那么决定您的表单应该拥有哪个类是一件简单的事情。

在 CSS 中,您可以执行以下操作:

form.main { ... }
/* main form rules */

form.other { ... }
/* other form rules */
于 2013-05-16T11:53:47.100 回答