PHP Classes

File: example_colors.php

Recommend this page to a friend!
  Classes of Thiemo Kreuz   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: 14 years ago
Size: 3,262 bytes
 

Contents

Class file image Download
<?php

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

require_once("Apeform.class.php");
$form = new Apeform();

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

$form->header("<hr>");

$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");

$form->header("<hr>");

$color3_base = array(
   
"Black", "Maroon", "Green", "Olive<br>",
   
"Navy", "Purple", "Teal", "Silver<br>",
   
"Gray", "Red", "Lime", "Yellow<br>",
   
"Blue", "Fuchsia", "Aqua", "White");
$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_base);
$color3_bichrome_index = $form->checkbox("Or choose from these colors", "",
   
$color3_bichrome);

$form->header("<hr>");

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

$form->header("<hr>");

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

$form->header("<hr>");

$form->submit();

// 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; }
</style>

<?=$form->toHTML()?>

<? 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>

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

  <p>
    <? if (! empty($color3_bichrome_index)): ?>
<? foreach ($color3_bichrome_index as $i): ?>
<span style="background-color: <?=preg_replace('/^[^:]*:(\w+).*$/i',
         
'\1', $color3_bichrome[$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>

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

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

<? endif; ?>