Sunday 8 June 2008

Object-oriented form rendering using PHP, part 2

There is one fundamental problem with my previous analysis of automatic form generation (using PHP).

// in the page header
$form->displayJS();
// ... lots of useful stuff
// in the page body
$form->display();
// ... other useful stuff

Which is: displaying the JS in the header is a really cool idea in a world where all forms are created on full HTML pages: with html, head, body tags. The truth of the matter is though that you often, probably mostly, work with PHP 'chunks' which you then include in the higher level page.

For instance, I am currently working on a login form which I would like to appear on several different pages. Displaying JS in the header using the displayJS() function becomes impossible because the include (using require_once, include, ...) happens when you are already in the 'body' of the resulting page.

<html>
<head>
<title>An example</title>
</head>
<body>
<?php require_once('login/login.php'); ?>
</body>
</html>

So, onto plan B. I have now changed my original idea to a more lightweight technique. On each 'input' you declare, you can add one or more client-side 'rules' which will take effect when the onchange event occurs (i.e. whenever the user validates input for the field). In other terms, whenever the user enters information into the field, it's trimmed, etc. using Javascript.
Once the form gets submitted, the onsubmit event triggered by the browser, will only make sure required fields are present, required being also used in the sense non-empty.

Right, so that's the theory. All it needs now is putting into practice.

Thoughts?

No comments:

Online Marketing
Add blog to our blog directory blog search directory Blog Directory Blogarama - The Blog Directory