Re: How to structure code for forms

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

on 06/08/2008 08:01 AM Ethan Whitt said the following:
> I am new to PHP and have been researching ways to structure code for forms.
> I have found a
> few basic tutorials that present, validate & present errors, and then
> process form data.  I was
> wondering if anyone could share their approach on how they structure these
> actions?  Ideally,
> I would like to separate each action in separate functions.  Thanks in
> advance...

I recommend a couple of things as crucial points to do proper forms
processing without inconviniences:

1. Present and process the forms with the same script. If the user
submits the form with invalid fields, you just use the same code in the
script to present the form again marking invalid fields.

2. Never output anything before presenting, validating and processing
the forms. If you start outputting any page data or even issue response
headers, you may not change the course of your script action later in
case there were validation errors or unexpected form processing errors
(like for instance database access failures).

Personally I use this popular forms validation class.

http://www.phpclasses.org/formsgeneration

It separates the forms handling in several steps like you want:

1. Define the input field types, initial values and validation rules
2. Load and filter any form submitted field values
3. Validate the loaded field values
4. Retrieve filtered and validated field values for form processing by
the application
5. Output the form if it is being presented for the first time or if it
has validation errors. It can use either HTML mixed with PHP, or Smarty
templates, or automatic layout plug-ins also available for this forms class.

Here you may watch a tutorial video that explains this approach with a
step by step fluxogram that makes it very clear:

http://www.phpclasses.org/browse/video/1/package/1/section/usage.html

This class is very mature. It is being developed since 1999 and is very
popular. Actually you could call it a framework because it comes with
many plug-ins for implementing sophisticated features such as AJAX form
submission, paged layouts, interconnected inputs, form page animation, etc..

Here you may find some demonstrative examples that you may try live:

http://www.meta-language.net/forms-examples.html

-- 

Regards,
Manuel Lemos

PHP professionals looking for PHP jobs
http://www.phpclasses.org/professionals/

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux