Re: HTML Forms, PHP Question

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

 



On Sun, 2006-11-19 at 11:00 -0500, Stephen wrote:
> Hi
> 
> First question to the list.
> 
> I am writing a control panel for a web site and there will be about 20 
> HTML forms.
> 
> Instead of creating 20 PHP files to process the submit data, is there a 
> way that I can do this with a single PHP file.
> 
> Either specify a specific function for each form, or use a case 
> statement somehow to direct to the function.


Yes...

<?php

$handlers = array
(
    'myLoginForm'        => 'handler_myLoginForm',
    'myRegistrationForm' => 'handler_myRegistrationForm',
    'myProfileForm'      => 'handler_myProfileForm',
    'myDonationForm'     => 'handler_myDonationForm',
);

$action = isset( $_POST['formName'] ) ? $_POST['formName'] : null;

if( isset( $handlers[$action] ) )
{
    $handlers[$action]( $_POST );
}

function handler_myLoginForm( $form )
{
    print_r( $form );
}

function handler_myRegistrationForm( $form )
{
    print_r( $form );
}

function handler_myProfileForm( $form )
{
    print_r( $form );
}

function handler_myDonationForm( $form )
{
    print_r( $form );
}

?>

Alternatively you can use the following style:

<?php

$action = isset( $_POST['formName'] ) ? $_POST['formName'] : null;

if( $action && function_exists( ($handler = 'handler_'.$action) ) )
{
    $handler( $_POST );
}

if( isset( $handlers[$action] ) )
{
    $handlers[$action]( $_POST );
}

function handler_myLoginForm( $form )
{
    print_r( $form );
}

function handler_myRegistrationForm( $form )
{
    print_r( $form );
}

function handler_myProfileForm( $form )
{
    print_r( $form );
}

function handler_myDonationForm( $form )
{
    print_r( $form );
}

?>

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- 
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