Zend - The PHP Company




Forms

Add Code


Formwizard  

Type: class
Added by: phpjuggler
Entered: 22/05/2002
Last modified: 08/12/2003
Rating: - (fewer than 3 votes)
Views: 11885
With this little class you can create update or insert forms. It's so easy and useful


<?php

        
// copyright  �  2000-2002 ilija injac injac@web.de
        // Authors: ilija injac
    
    
class formwizard
    
{
        
//Members
        
var $_formFields;         //Variable holding the names of the formfields
        
var $_formValues;         //Variable holding the values of the fields (for edit forms)
        
var $_formName;              //Variable holding the name of the form
        
var $_formAction;        //Variable holding the form action
        
var $_formMethod;        //Get or Post Form ?
        
var $_fieldCaptions;    //The captions for the forms
        
var $_isInsertForm;     //is the form a insert form ?
        
var $_nil;                //a tempvar
        
        //constructor
        
function formwizard($formFields,$formName,$formAction,$formMethod,$fieldCaptions,$formValues="")
        {
            
//init Members
            
$this->_formFields         $formFields;
            
$this->_formName        $formName;
            
$this->_formAction        $formAction;
            
$this->_formMethod      $formMethod;    
            
$this->_fieldCaptions    $fieldCaptions;
            
            
//check if edit or add form
            
if($formValues == "")
            {
                
//this is a insert form
                
$this->_isInsertForm true;
                
//get the values for the form elements
                
$this->get_insert_form_properties();
            }else
            {
                
//this is a edit form
                
$this->_isInsertForm     false;
                
//insert member for form values
                
$this->_formValues         $formValues;
                
//get the values for the form elements
                
$this->get_edit_form_properties();
            }
                        
        }
        
        
//get values for form fields, if edit form
        
function get_edit_form_properties()
        {
            
            
//get the form names
            
$this->_nil explode("|",$this->_formFields);
            
//exchange the value
            
$this->_formFields $this->_nil;
            
            
//get the form values
            
$this->_nil explode("|",$this->_formValues);
            
$this->_formValues $this->_nil;
            
            
//get the field captions
            
$this->_nil explode("|",$this->_fieldCaptions);
            
$this->_fieldCaptions $this->_nil;
            
        }
        
        
//get field names for form fields, if insert form
        
function get_insert_form_properties()
        {
            
//get the form names
            
$this->_nil explode("|",$this->_formFields);
            
//exchange the value
            
$this->_formFields $this->_nil;    
                        
            
//get the field captions
            
$this->_nil explode("|",$this->_fieldCaptions);
            
$this->_fieldCaptions $this->_nil;
        }
        
        
        function 
print_form()
        {
            
            echo 
"<form name="$this->_formName" method="$this->_formMethod" action="$this->_formAction">";
            echo 
"<table border=0 cellspacing=0 cellpadding=3>";
            
            
//check if insert or update form
            
if($this->_isInsertForm)
            {
                
//insert form
                
foreach($this->_formFields as $key => $value)
                {
                    
                    echo 
"<tr><td>".$this->_fieldCaptions[$key]."</td><td><input type="text" name=".".$this->_formFields[$key].""></td></tr>";
                }
                
                echo 
"<tr><td><input type="submit" value="go"></td><td><input type="reset" value="reset"></td></tr>";
                echo 
"</table>";
                echo 
"</form>";
            }else
            {
                
//update form
                
foreach($this->_formFields as $key => $value)
                {
                    echo 
"<tr><td>".$this->_fieldCaptions[$key]."</td><td><input type="text" name=".".$this->_formFields[$key]."" value="".$this->_formValues[$key].""></td></tr>";
                }
                echo 
"<tr><td><input type="submit" value="go"><td>&nbsp;</td></tr>";
                echo 
"</table>";
                echo 
"</form>";
            }            
            
        }
        
        
    }

?>


Usage Example


include("lib/form_wizard_class.inc");
    
    $fields     = "name|vorname|strasse|plz|ort";
    $captions    = "Name|Vorname|strasse|Plz|Ort";
    $values        = "Injac|Ilija|blubb|33323|blubb";
    
    //edit form
    $form1    = new formwizard($fields,"testform","test.php","post",$captions,$values);
    $form1->print_form();
    //insert form
    $form2    = new formwizard($fields,"testform","test.php","post",$captions);
    $form2->print_form();


Rate This Script





Search



This Category All Categories