PHP Classes
elePHPant
Icontem

File: example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Kacper Rowinski  >  FormGenerator  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example script
Class: FormGenerator
Generate and validate Web forms
Author: By
Last change:
Date: 2 years ago
Size: 3,067 bytes
 

Contents

Class file image Download
<?php
/*
 * This is a simple example how to user formGenerator class
 */
include('formGenerator.php');

// to change css style
define('STYLE', 1);

// to show image for captcha
if (isset($_GET['captcha']))
{
   
formGenerator::getCaptcha();
}

function
createHtml(&$pFrom)
{
   
$html = '<html>';

   
$html .= '<head>';
    if (
1 == STYLE)
    {
       
$html .= '<link rel="stylesheet" type="text/css" href="css/minimalistic_form.css"/>';
    }
    else
    {
       
$html .= '<link rel="stylesheet" type="text/css" href="css/other_form.css"/>';
    }
   
$html .= '</head>';

   
$html .= '<body style="width: 800px; margin-left: auto; margin-right: auto;">';

   
$html .= $pFrom;

   
$html .= '</body>';
   
$html .= '</html>';

    return
$html;
}

// Textarea test
$formGenerator = new formGenerator('Test Textarea');
$formGenerator->AddInput('text area', 'text_area', 'textarea', 'TEXT_NO_EMPTY-360-2', 'Invalid comment must be between 2 to 360 chars');
$formGenerator->AddButton('submit', 'test it!');
if (
false === $formGenerator->IsSubmited() and false === $formGenerator->ValidateData())
{
   
var_dump($formGenerator->getKey('text_area'));
}
$html .= $formGenerator->getForm();

// Login test
$formGenerator = new formGenerator('Test Login');
$formGenerator->AddInput('login', 'login', 'text', 'TEXT_NO_EMPTY', 'Login invalid');
$formGenerator->AddInput('password', 'password', 'password', 'TEXT_NO_EMPTY', 'Password invalid');
$formGenerator->AddCustomizedHTML('<li style="text-align: right; padding-right: 16px; font-size: 12px"><a href="$">Lost your password?</a></li>');
$formGenerator->AddButton('submit', 'test login!');
if (
false === $formGenerator->IsSubmited() and false === $formGenerator->ValidateData())
{
   
var_dump($formGenerator->getKey('login'));
   
var_dump($formGenerator->getKey('password'));
}
$html .= $formGenerator->getForm();

// Captcha test
$formGenerator = new formGenerator('Test Captcha');
$formGenerator->AddCaptcha('Incorrect captcha!', 'captcha', 'example.php?captcha=true');
$formGenerator->AddButton('submit', 'captcha ok ?');
if (
false === $formGenerator->IsSubmited() and false === $formGenerator->ValidateData())
{
   
var_dump($formGenerator->getKey('captcha'));
}
$html .= $formGenerator->getForm();

// Select and checkbox test
$formGenerator = new formGenerator('Test Select');
$formGenerator->AddInput('simple select', 'input_select', 'select', 'TEXT_NO_EMPTY', 'Please option');
foreach (array(array(
'id' => 1, 'name' => 'first'), array('id' => 2, 'name' => 'second')) as $values)
{
   
$formGenerator->AddOptions($values['id'], $values['name']);
}
$formGenerator->AddInput('simple checkbox', 'name_checkbox', 'checkbox', 'CHECKBOX', 'Please select checkbox');
$formGenerator->AddButton('submit', 'show select');
if (
false === $formGenerator->IsSubmited() and false === $formGenerator->ValidateData())
{
   
var_dump($formGenerator->getKey('input_select'));
   
var_dump($formGenerator->getKey('name_checkbox'));
}
$html .= $formGenerator->getForm();

// show data
echo createHtml($html);