Login   Register  
PHP Classes

File: example_colors.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Thiemo Mättig  >  TM::Apeform  >  example_colors.php  >  Download  
File: example_colors.php
Role: Example script
Content type: text/plain
Description: Ideas for selecting a color in a form
Class: TM::Apeform
A very abstract web form builder and processor
Author: By
Last change: Accessible without user login
Date: 5 years ago
Size: 3,262 bytes


Class file image Download

 * Color form example.
 * @author Thiemo Mättig (http://maettig.com/)

$form = new Apeform();

$color1_rgb $form->checkbox("Choose one or more colors""",
$color1_name $form->text("Or type another color",
"Leave empty if not required");


$color2_select $form->select("Choose a color""""Red|Green|Blue");
$name_select $form->getName();
$color2_name $form->text("Or type a new color",
"Leave empty if not required");


$color3_base = array(
$color3_bichrome = array(
'<span style="background:maroon;color:white">dark red</span>',
'<span style="background:red;color:white">red</span>',
'<span style="background:green;color:white">dark green</span>',
'<span style="background:lime">green</span>');
$color3_base_index $form->checkbox("Choose one or more colors""",
$color3_bichrome_index $form->checkbox("Or choose from these colors""",


$form->header("Type a color name in the text field below");
$color4 $form->text();


$color5 $form->text("#RRGGBB""", array("00""99""FF"), 2);
$name_rgb $form->getName();



// Here the construction and validation of the form ends. End of "controller"
// code, start of "view" code.


<style type="text/css">
/* The :first-child and + selectors do not work in Internet Explorer. */
#<?=$name_select?>i option:first-child { background-color: #F99; }
#<?=$name_select?>i option+option { background-color: #9F9; }
#<?=$name_select?>i option+option+option { background-color: #99F; }
#<?=$name_rgb?>i0 { background-color: #FCC; }
#<?=$name_rgb?>i1 { background-color: #CFC; }
#<?=$name_rgb?>i2 { background-color: #CCF; }


<? if ($form->isValid()): ?>

  <p style="background-color: <?=empty($color1_name) ? "#" .
in_array("Red",   $color1_rgb) ? "FF" "00") .
in_array("Green"$color1_rgb) ? "FF" "00") .
in_array("Blue",  $color1_rgb) ? "FF" "00") : $color1_name?>;">
    Color No. 1

  <p style="background-color: <?=empty($color2_name) ? $color2_select :
    Color No. 2

    <? if (! empty($color3_bichrome_index)): ?>
      <? foreach ($color3_bichrome_index as $i): ?>
        <span style="background-color: <?=preg_replace('/^[^:]*:(\w+).*$/i',
          Color No. 3</span>
      <? endforeach; ?>
    <? else: ?>
      <? foreach ($color3_base_index as $i): ?>
        <span style="background-color: <?=strip_tags($color3_base[$i])?>;">
          Color No. 3</span>
      <? endforeach; ?>
    <? endif; ?>

  <p style="background-color: <?=$color4?>;">
    Color No. 4

  <p style="background-color: <?vprintf("#%02s%02s%02s"$color5)?>;">
    Color No. 5

<? endif; ?>