Like many web developers I do forms all the time. I found myself doing the same all the time: placing input fields, assigning a name to each, ajax the form, then create the PHP which involves to assign a PHP var to each $_REQUEST['var'], escape and validate data, build the html and emailing the results.

So I found that 70% of the work is duplicated but I just can't duplicate a page and change the fields. I end up wasting more time reformatting, deleting and adding different fields than creating from scratch.

I started planing to program a "list of IDs to html+php" converter in which I'd input all the IDs and this would output the basic html and php. Then I thought: there's got to be thousands of developers that go through this, I'd be reinventing the wheel. So this is my question, I'm trying to find that wheel that somebody must have invented already.

I found this: http://www.trirand.com/blog/jqform/ which does more or less what I'm looking for but it's an expensive solution and it has too much functionality for what I'd be using it.

Which tools do you use to optimize repetitive task about HTML and PHP?

  • Juan: have you find something useful for this? – Nicolás Dec 3 '11 at 23:40

A solution that addresses part of your problem is using PEAR::QuickForm.

With it, you can build quick and easy a form, just by using a simply OOP syntax. You can look at its Tutorial to see how it works.

  • I forgot to mention that the desired tool should generate html. This is aimed to automated small static website without php needed. Thanks anyway – Juan Nov 23 '11 at 18:56
  • 1
    QuickForm does generate HTML output, it's its main aim by the way. – Nicolás Nov 23 '11 at 19:07

It's not so much about finding tools to help you generate repetitive PHP/HTML as about designing your app to avoid repetitive code in the first place (whether you have to write it by hand or generate it with something else).

Usually this is done by using a UI library or framework. For instance, in CakePHP, there is the FormHelper and built-in functionality in view, controller, and models to handle forms automagically.

So the developer doesn't need to create the HTML for the form or even the PHP to validate and process the form. All you have to do is define your models (what fields, the types, and the validation rules to use) and then use the form helper to output the form like so:

echo $this->Form->create();
echo $this->Form->input('field1'); // Note: these are the fieldnames from the
echo $this->Form->input('field2'); // model, not the input name or ID, which
echo $this->Form->input('field3'); // will be generated as ModelField and
echo $this->Form->input('field1'); // data[Modelname][fieldname], respectively
echo $this->Form->end('Submit Me!');

Then in your controller, you can save your form data with a single line of code, without having to worry about what fields or validation rules this form has.

To add AJAX functionality, you can then use AJAX helpers for specific AJAX libraries to progressively enhance your forms. However, this too should be accomplished with a few short lines of code, and ought not require that you pass input names or IDs. Ideally, you should be able to do something like this:


And it would ajaxify all forms. And this line of code could be included in an external JS file that's used by all form pages to initialize the AJAX UI, instead of each page having field-specific client-side validation or AJAX code.

Your Answer

