I've got a small application in which i've 9 variations, and each form possess a least 40+ fields in which the user must enter some data.

I've written each form in one php file making an expert PHP file where i only add the shape in the center of the page.

My real question is:

I'd rather not write 9 different function for every form coz it might be too lengthy and that i don't believe it is the best practice, so it is possible to way or idea that will help me create a general function by which i'm able to pass some variables to and add the information towards the db.

Can one like iterate with the $_Publish array and extarct the information and reformat it in some way? however again every area possess a different title and that i appears such as this is out of the question.

Try by doing this:

<?php

abstract class FormData
{
    const BOOLEAN = 'bool';
    const INTEGER = 'int';
    const FLOAT = 'float';
    const STRING = 'string';
    protected $_types = array();

    public static function create($data)
    {
    	$action = isset($data['action']) ? $data['action'] : '';
    	switch ($action)
    	{
    		case 'form1': return new MyForm1($data);
    		default: return null;
    	}
    	return null;
    }

    protected function loadPostVars($data)
    {
    	foreach ($data AS $var=>$value)
    	{
    		$value = $this->convertVar($var, $value);
    		if (!is_null($value) && property_exists($this, $var))
    		{
    			$this->$var = $value;
    		}
    	}
    }
    protected function convertVar($var, $value)
    {
    	if (array_key_exists($var, $this->_types))
    	{
    		$type = $this->_types[$var];
    		switch ($type)
    		{
    			case FormData::BOOLEAN: return (bool)(int)$value;
    			case FormData::INTEGER: return (int)$value;
    			case FormData::FLOAT: return (float)$value;
    			case FormData::STRING: // drop down
    			default:
    				return myEscapeString($value);
    		}
    	}
    	return null;
    }
}

class MyForm1 extends FormData 
{
    public $fld1;
    public $fld2;
    public $fld3;
    // etc...

    public function __construct($data)
    {
    	$this->_types = array(
    		'fld1' => FormData::INTEGER,
    		'fld2' => FormData::STRING,
    		'fld3' => FormData::BOOLEAN,
    	);
    	$this->loadPostVars($data);
    }
}

// And finally process your form data
// You should add hidden input 'action' to each form to identify the form
if ($form = FormData::create($_POST))
{
    echo $form->fld1, $form->fld2, $form->fld3;
}
else 
{
    exit('error: unknown action provided');
}

?>

This solution needs to be enhanced - I have written it extremely fast. However I hope you'll catch the primary idea. Hope this helps. Sure, in every form class you can include specific techniques to process the request etc.

You may be thinking about something similar to Zend_Form.

I have used such like in a single of my projects. Essentially, I define all of the fields, including how you can display them, what data type they've (number, date, etc), in addition to what related to them once published back. Then you definitely pass that information to 1 function which creates a HTML form. When that form is posted, you pass exactly the same information to another function which works the required tasks.

During my specific situation, it had been used just to produce search forms, therefore the processing involved developing a Choose sql statement. Here is a sample of among the area definitions:

$criteria = array(
    array("label" => "Search clients",
          "type"  => "radio",
          "values"=> array("1" => "Just " . $client->getName(),
                           "2" => "All clients"),
          "where" => array("1" => "c.`clientId` = " . $client->getId(),
                           "2" => "1"), // always true
          "default" => "1"),
    array("label" => "Last Name",
          "type"  => "text",
          "where" => "s.`lastName` LIKE '%s'"),
    array("label" => "First Name",
          "type"  => "text",
          "where" => "s.`firstName` LIKE '%s'")
    // etc...

Just 2 small ideas..

1) Would you show all 9 forms in a single page? If so - I reeeally don't believe it's wise.
2) Why not divide your forms to various pages and provide same names to fields in your forms where it's possible?

You are able to iterate around the $_Publish array having a foreach.

   foreach($_POST as $name => $value) {
      echo $name, ' ', $value, '<br />';
   }