Frames Modems Help Home Page Chipsets Search No Frames
Diary Entries See also Site Info & Diary.
11 November 2001 A free gift for PHP4 users that build Form controls...

(see 18 Dec for a Class implementation)

  It's become apparent, as I write php pages to build the manufacturer pages, that I'm repetitively writing the same Form controls. PHP allows both Include files (see 4 Oct) and user-functions. I've thus spent each evening this week producing an include file of functions to build Form controls. Only fFile(), fImage() & fButton() are not there (these are left as exercises for the reader - I've always wanted to say that).

To save space, the meaning of each function parameter is detailed just once. The same parameter names are used throughout. fInput is intended to be a private function, used only internally, and hence is placed last.

Here's a little scrap of PHP to show how a sample of one of the functions (a SELECT control) is called:

  $arr=array(1=>"Option 1", 3=>"Option 3", 7=>"Option 7");
  echo "<TR>\n".
   fSelect("testSelect", $arr, "Choose an option...", "y", "lnk",
     "Options:", "e", "O").
   "</TR>\n";
 
<?php
/* an include file of PHP4 functions for use in Form controls creation

  if both a LABEL and an ID are included the control & LABEL will be
  wrapped in separate TABLE data-delimiters (<TD>..</TD>).
  otherwise these delimiters are NOT included

  the functions include CLASSes used in my new website Style Sheet.
  These will need to be removed or replaced by your own.

  $met  string, method of FORM (GET or POST)
  $url  string, URL to send FORM data to
  $acc  char, allows short-cut access to form
  $cls  string, style-sheet class
  $chk  anything (is CHECKED) or null
  $col  integer, (TEXTAREA only)
  $dis  anything (is DISABLED) or null
  $id  unique string, used only with $lab
  $inp  INPUT or SELECT or TEXTAREA
  $is2  anything (uses key+value) or null (use value only) (SELECT only)
  $lab  string, label identifier (see also $id & $rt)
  $max  integer, max allowed input (Text & Password)
  $mul  anything (allow multiple selections) or null (SELECT only)
  $nam  string, required ALL functions
  $opt  array of Options (SELECT only) (see also $is2, $sel & $mul)
  $row  integer, (TEXTAREA only)
  $rdo  anything (is READONLY) or null
  $rt  anything (Label to right of control) or null (to left)
  $sel  string, text to show on first (null-key) option (SELECT only)
  $siz  integer
  $tab  integer, tab-order on Form
  $tit  string, TITLE (implemented as a tooltip on visual browsers)
  $txt  string, default entry (TEXTAREA only)
  $typ  string, type of INPUT
  $val  anything (depends on context)

  written by Alex Kemp, 11 Nov 2001
  updated 29 Nov 2001 (TITLEs added)
  http://www.modem-help.co.uk
*/
  function fCheckbox($nam, $val, $lab="", $id="", $acc="", $rt="", $tit="",
   $chk="", $tab="", $dis="") {
   return fInput($acc, $cls="dis", $chk, $dis, $id, $lab, "", $nam, "",
     $rt, "", $tab, $tit, $typ="checkbox", $val);
  }

  function fHidden($nam, $val) {
   return fInput("", "", "", "", "", "", "", $nam, "", "", "", "", "",
     $typ="hidden", $val);
  }

  function fForm($nam, $url, $met="GET") {
   return "<FORM METHOD=$met ACTION='$url' NAME='$nam'>\n";
  }

  function fPassword($nam, $cls="lnk", $lab="", $id="", $acc="", $rt="",
   $tit="", $siz="40", $tab="", $max="", $dis="") {
   return fInput($acc, $cls, "", $dis, $id, $lab, $max, $nam, "", $rt,
     $siz, $tab, $tit, $typ="password", "");
  }

  function fRadio($nam, $val, $lab="", $id="", $acc="", $rt="", $tit="",
   $chk="", $tab="", $dis="") {
   return fInput($acc, $cls="dis", $chk, $dis, $id, $lab, "", $nam, "",
     $rt, "", $tab, $tit, $typ="radio", $val);
  }

  function fReset($val=null, $acc="", $tit="", $tab="") {
   return fInput($acc, "", "", "", "", "", "", "", "", "", "", $tab,
     $tit="", $typ="reset", $val);
  }

  function fSearch($nam, $sNam, $val="Search", $cls="lnk", $siz="40",
   $tit="", $rt="") {
   if($rt) {
     return fSubmit($sNam, $val, "", $tit)." ".
      fText($nam, $cls, $siz, "", "", "", "", $tit);
   } else {
     return fText($nam, $cls, $siz, "", "", "", "", $tit)." ".
      fSubmit($sNam, $val, "", $tit);
   }
  }

  function fSelect($nam, $opt, $sel="", $is2='y', $cls="lnk", $lab="", $id="",
   $acc="", $rt="", $tit="", $dis="", $tab="", $mul="", $siz="") {
   return fInput($acc, $cls, "", $dis, $id, $lab, "", $nam, "", $rt, $siz,
     $tab, $tit, "", "", "SELECT", "", "", "", $opt, $is2, $sel, $mul);
  }

  function fSubmit($nam, $val=null, $acc="", $tit="", $tab="", $dis="") {
   return fInput($acc, "", "", $dis, "", "", "", $nam, "", "", "", $tab,
     $tit, $typ="submit", $val);
  }

  function fText($nam, $cls="lnk", $siz="40", $lab="", $id="", $acc="", $rt="",
   $tit="", $val=null, $dis="", $tab="", $max="", $rdo="") {
   return fInput($acc, $cls, "", $dis, $id, $lab, $max, $nam, $rdo, $rt,
     $siz, $tab, $tit, $typ="text", $val);
  }

  function fTextArea($nam, $cls="lnk", $col="40", $row="3", $lab="", $id="",
   $acc="", $rt="", $tit="", $dis="", $tab="", $txt="", $rdo="") {
   return fInput($acc, $cls, "", $dis, $id, $lab, "", $nam, $rdo, $rt, "",
     $tab, $tit, "", "", "TEXTAREA", $col, $row, $txt);
  }

  function fInput($acc, $cls, $chk, $dis, $id, $lab, $max, $nam, $rdo, $rt,
   $siz, $tab, $tit, $typ, $val, $inp="INPUT", $col=1, $row=1, $txt="",
   $opt="", $is2="", $sel="", $mul="") {
   $ret="<$inp";
   if($dis) $cls="dis";
   if($cls) $ret.=" CLASS='$cls'";
   if($typ) $ret.=" TYPE='$typ'";
   if($nam) $ret.=" NAME='$nam'";
   if($col>1) $ret.=" COLS='$col'";
   if($row>1) $ret.=" ROWS='$row'";
   if($siz) $ret.=" SIZE='$siz'";
   if($max) $ret.=" MAXLENGTH='$max'";
   if($acc && !$id) $ret.=" ACCESSKEY='$acc'";
   if($tab) $ret.=" TABINDEX='$tab'";
   if($id) $ret.=" ID='$id'";
   if($val!==null) $ret.=" VALUE='$val'";
   if($tit) $ret.=" TITLE='$tit'";
   if($chk) $ret.=" CHECKED";
   if($dis) $ret.=" DISABLED";
   if($mul) $ret.=" MULTIPLE";
   if($rdo) $ret.=" READONLY";
   $ret.=">";
   if($opt) {
     $txt="\n";
     if($sel) $txt.="<OPTION SELECTED VALUE=''>$sel</OPTION>\n";
     if($is2) {
      foreach ($opt as $key=>$value) {
        $txt.="<OPTION VALUE='$key'>$value</OPTION>\n";
      }
     } else {
      foreach ($opt as $value) {
        $txt.="<OPTION>$value</OPTION>\n";
      }
     }
   }
   if($inp!="INPUT") $ret.=$txt."</$inp>";
   if($lab) {
     if($acc) $lab=ereg_replace($acc, "<U>$acc</U>", $lab);
     $label=($acc && $id)?"LABEL ACCESSKEY='$acc'":"LABEL";
     if($id) {
      if($rt) {
        $ret=
        "<TD CLASS='rat'>$ret</TD>\n".
        "<TD CLASS='lat'><$label FOR='$id'>$lab</LABEL></TD>\n";
      } else {
        $ret=
        "<TD CLASS='rat'><$label FOR='$id'>$lab</LABEL></TD>\n".
        "<TD CLASS='lat'>$ret</TD>\n";
      }
     } else {
      if($rt) {
        $ret="<$label>$ret\n$lab\n</LABEL>\n";
      } else {
        $ret="<$label>$lab\n$ret\n</LABEL>\n";
      }
     }
   }
   return $ret;
  }
?>