PHP
  Home arrow PHP arrow Page 6 - Making PHP Forms Object-Oriented
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Making PHP Forms Object-Oriented
By: Yuri Makassiouk
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 29
    2003-01-23

    Table of Contents:
  • Making PHP Forms Object-Oriented
  • Analysis
  • Executing Environment
  • Implementing the FormProcessor Class
  • Inputs - Brief Introduction
  • Example of a Form
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Making PHP Forms Object-Oriented - Example of a Form


    (Page 6 of 7 )

    Now we are equipped for the final touch: building a small example. I will demonstrate creation of the first page of a wizard that should collect some information. Let's say, about a subscription to some internet resource. As the first step, the user must enter the username he/she is going to use and a valid email address, then select the type of the registration. Based on the registration type the user chooses, the wizard should proceed to a corresponding screen.

    Before the wizard can proceed, it validates values that the user enters. I will not build the forms for the second step. There's not much interesting to do without introducing additional input classes; and I would still like the article's subject to stay on the same topic.

    <?php

      define('MinNameLength', 5);
      
      class RegForm01 extends FormProcessor {
        
        function CustomCheck() {
          if (strlen($this->Values['Name']) < MinNameLength)
            $this->Errors['Name'] =  
                'Username should contain at least ' . MinNameLength . ' symbols';
            
          if ($this->Values['Email']) {
            if (!eregi('^[-!#$%&\'*+\\./0-9=?A-Z^_\`a-z{|}~]+
                @[-!#$%&\'*+\\/0-9=?A-Z^_\`a-z{|}~]+\.[-!#$%&'*+
                '\\./0-9=?A-Z^_\`a-z{|}~]+$', $this->Values['Email']))
              $this->Errors['Email'] = 'Value entered does not
                appear to be a valid e-mail address';
          } else {
            $this->Errors['Email'] =  
                'E-mail address is required to complete subscription';
          }
        }
        
        function NextWizardPage() {
          //decision about what wizard's screen to load next is made here,
          //based on the submitted values:
          return ($this->Values['PaymentMethod'] ==  
            'P')?'reg_form_free.php':'reg_form_payed.php';
        }
        
        function StoreData() {
          // we will not discuss storing persistent variables in this article.
          // here would normally appear code that updates a database or
          // stores session variables
          STORE_VARIABLE($this->Values['Name']);
        }
        
        function DisplayForm() {
        // I'm not sure you meet a form like this on a real web-site =), but I hope
        // it'll do for the example

    ?>
    <form action="<?php echo $PHP_SELF?>" method="POST">
        Username: <?php new TextInput($this, 'Name', '', 'size="30" maxlength="100"')?>
        <?php $this->ErrorReport('Name')?>

        Email: <?php new TextInput($this, 'Email', '', 'size="30" maxlength="100"')?>
        <?php $this->ErrorReport('Email')?>
        <br><br>
        Subscription type:<br>
        <?php new RadioButtons($this, 'SubscrType', array('P'=>'Preview', 'M'=>'Membership'), 'P')?>
        <?php $this->ErrorReport('SubscrType')?>
        <br>
        <br>
        <?php $this->Additional()?>
        <input type="submit" name="Submit" value="Submit">
    </form>
    <?php

        }
      }
      
      $form = new RegForm01('RegForm01', 'reg_form01.php');
    ?>


    This code can also be downloaded at the end of this article.

    More PHP Articles
    More By Yuri Makassiouk


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT