Home    TrueColor    MyImage    About Allayers

Linking multiple forms and submitting to handler script

This script has it's root is daily practise. A webdesign company used a mail-to Perl script to submit request forms to. They wanted a simple solution for handling multiple forms. Using the form linker class it required verly little scripting from the calling page to realize this. It registers HTML form templates and includes them with addition of an extra hidden element for use as index.

<?
// create object
include("linker.class.php");
$formlinker = new linker();

// these have to be set!
$formlinker->mailaction="./postpage.php";
// hidden settings for submit processor
$formlinker->mailhidden="./mailhidden.html";

// the form templates have to added
$formlinker->addform("./form1.html");
$formlinker->addform("./form2.html");
// etc.
// output correct form
$formlinker->writeform();
?>

This demo shows how forms can be chained together this way and submitted in the end.



Form Linker Class

<?
class linker{
      
// private
      
var $forms;
      var
$index;
      var
$previous;
      
// have te be set
      
var $mailaction;
      var
$mailhidden;

      
// private methods

      
function linker(){
      GLOBAL
$HTTP_POST_VARS;

               if (
$HTTP_POST_VARS)
                    
$this->getpostvars($HTTP_POST_VARS);
                    else
                    
$this->index = 0;
               }

      function
getpostvars(&$vars){

               while (list(
$id,$item)=each($vars)){
                      if (
$id=="currentformindex")
                           
$this->index = $item;
                           else
                           
$this->previous .= "<input type=\"hidden\" name=\"".$id."\" value=\"".$item."\">\n";
                      }

               }

      function
getheader(){
      GLOBAL
$PHP_SELF;
               if (
$this->index == sizeof($this->forms)-1){
                  echo
"<FORM METHOD=\"POST\" NAME=\"last\" ACTION=\"".$this->mailaction."\">\n";
                  include(
$this->mailhidden);
                  }else{
                     echo
"<FORM METHOD=\"POST\" NAME=\"aform\" ACTION=\"".$PHP_SELF."\">\n";
                     echo
"<INPUT TYPE=\"hidden\" name=\"currentformindex\" value=\"".($this->index + 1)."\">\n";
                     }
               }
      
// public
      
function addform($html){
               
$this->forms[] = $html;
               }

      function
writeform(){
               if (
$this->mailaction && $this->mailhidden){
                    
$this->getheader();
                    echo
$this->previous;
                    include(
$this->forms[$this->index]);
                    echo
"</FORM>";
                    }else{
                       echo
"Set mailaction (script path) and mailhidden file path (script mail parameters)";
                       }
               }

      }
//end class
?>


Other PHP samples: Simple Meta Content Refresh Slideshow and Image Browser



Allayers - Copyright 2003 - Paul Barends