Home arrow PHP arrow Page 6 - Making PHP Forms Object-Oriented
PHP

Making PHP Forms Object-Oriented


In this article Yuri explains an object oriented way to create and use forms on your web site.

Author Info:
By: Yuri Makassiouk
Rating: 4 stars4 stars4 stars4 stars4 stars / 40
January 23, 2003
TABLE OF CONTENTS:
  1. · Making PHP Forms Object-Oriented
  2. · Analysis
  3. · Executing Environment
  4. · Implementing the FormProcessor Class
  5. · Inputs - Brief Introduction
  6. · Example of a Form
  7. · Conclusion

print this article
SEARCH DEVARTICLES

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.
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials