Accueil > > > [PHP5] CLASSE FORMULAIRE
[PHP5] CLASSE FORMULAIRE
Information sur la source
Description
Une petite classe formulaire, avec gestion de tous les éléments et attributs possible liés à un formulaire (y compris legend, label, fieldset). Utilisation de la méthode magique __toString () pour afficher le formulaire créé.
Source
- <?php
- class form {
-
-
- // propriétés privées : tous les éléments et attributs utilisables (certaines valeurs sont entrées par défaut)
- private $eventArr = array ('onfocus' => '',
- 'onblur' => '',
- 'onselect' => '',
- 'onchange' => '',
- 'onclick' => '',
- 'ondblclick' => '',
- 'onmousedown' => '',
- 'onmouseup' => '',
- 'onmouseover' => '',
- 'onmousemove' => '',
- 'onmouseout' => '',
- 'onkeypress' => '',
- 'onkeydown' => '',
- 'onkeyup' => '');
- private $commonArr = array ('id' => '',
- 'class' => '',
- 'title' => '',
- 'style' => '',
- 'dir' => '',
- 'lang' => '',
- 'xml:lang' => '');
- private $formArr = array (
- 'method' => 'post',
- 'action' => '',
- 'id' => 'mainForm',
- 'enctype' => 'application/x-www-form-urlencoded',
- 'accept' => '',
- 'onsubmit' => '',
- 'onreset' => '',
- 'accept-charset' => 'unknown',
- 'style' => ''
- );
- private $inputArr = array ('text' => array ('value' => '',
- 'name' => '',
- 'alt' => '',
- 'tabindex' => '',
- 'accesskey' => '',
- 'readonly' => '',
- 'disabled' => '',
- 'width' => '',
- 'maxlength' => '',
- 'size ' => ''),
- 'button' => array ('name' => '',
- 'value' => '',
- 'alt' => '',
- 'tabindex' => '',
- 'accesskey' => '',
- 'disabled' => ''),
- 'hidden' => array ('name' => '',
- 'value' => '',
- 'alt' => '',
- 'disabled' => '',
- 'size ' => ''),
- 'password' => array ('name' => '',
- 'value' => '',
- 'alt' => '',
- 'tabindex' => '',
- 'accesskey' => '',
- 'readonly' => '',
- 'disabled' => '',
- 'width' => '',
- 'maxlength' => '',
- 'size ' => ''),
- 'submit' => array ('name' => '',
- 'value' => '',
- 'alt' => '',
- 'tabindex' => '',
- 'accesskey' => '',
- 'disabled' => ''),
- 'checkbox' => array ('name' => '',
- 'value' => '',
- 'alt' => '',
- 'tabindex' => '',
- 'accesskey' => '',
- 'disabled' => '',
- 'checked' => ''),
- 'radio' => array ('name' => '',
- 'value' => '',
- 'alt' => '',
- 'tabindex' => '',
- 'accesskey' => '',
- 'disabled' => '',
- 'checked' => '',
- 'title' => ''),
- 'reset' => array ('name' => '',
- 'class' => '',
- 'value' => '',
- 'alt' => '',
- 'tabindex' => '',
- 'accesskey' => '',
- 'disabled' => '',
- 'title' => ''),
- 'file' => array ('name' => '',
- 'value' => '',
- 'alt' => '',
- 'tabindex' => '',
- 'accesskey' => '',
- 'disabled' => '',
- 'accept' => '',
- 'size ' => ''),
- 'image' => array ('name' => '',
- 'value' => '',
- 'alt' => '',
- 'tabindex' => '',
- 'accesskey' => '',
- 'disabled' => '',
- 'src' => '',
- 'usemap' => '',
- 'ismap' => '')
- );
- private $fieldsetArr = array ();
- private $pArr = array ();
- private $legendArr = array ();
- private $labelArr = array ('for' => '');
- private $textareaArr = array ('rows' => '',
- 'cols' => '',
- 'disabled' => '',
- 'readonly' => '',
- 'accesskey' => '',
- 'tabindex' => '',
- 'name' => '');
- private $selectArr = array ('disabled' => '',
- 'multiple' => '',
- 'size' => '',
- 'name' => '');
- private $optionArr = array ('disabled' => '',
- 'label' => '',
- 'selected' => '',
- 'value' => '');
- private $optgroupArr = array ('disabled' => '');
- private $formBuffer = array ();
- private $formElementArr = array ();
- private $formAttributeArr = array ();
-
- //Constructeur
- public function __construct () {
-
- }
-
- // débuter effectivement le formulaire
- public function openForm ($arrArgs = array ()) {
- foreach ($this -> formArr as $clef => $val) {
- if (array_key_exists ($clef, $arrArgs)) {
- $this -> formAttributeArr[$clef] = $arrArgs[$clef];
- }
- else if (!empty ($val)) {
- $this -> formAttributeArr[$clef] = $val;
- }
- }
- $this -> formBuffer['open'] = '<form ';
- foreach ($this -> formAttributeArr as $clef => $val) {
- $this -> formBuffer['open'] .= $clef.'="'.$val.'" ';
- }
- $this -> formBuffer['open'] .= '>';
- }
-
- // fermer le formulaire
- public function closeForm () {
- $this -> formBuffer['close'] = '</form>';
- }
-
- // ajouter un type input
- public function addInput ($elem, $arrArgs = array ()) {
- if (!array_key_exists ($elem, $this -> inputArr)) {
- throw new Exception ($elem.' n\'est pas un élément valide');
- }
- if (!array_key_exists ('name', $arrArgs) && $elem !== 'submit' && $elem !== 'reset') {
- $arrArgs['name'] = 'default';
- }
- $cpt = count ($this -> formElementArr);
- $this -> formElementArr[$cpt][$elem] = array ();
- $arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> inputArr[$elem]);
- foreach ($arrTemp as $clef => $val) {
- if (array_key_exists ($clef, $arrArgs)) {
- $this -> formElementArr[$cpt][$elem][$clef] = $arrArgs[$clef];
- }
- }
- $chaineTemp = '<input type="'.$elem.'" ';
- foreach ($this -> formElementArr[$cpt][$elem] as $clef => $val) {
- $chaineTemp .= $clef.'="'.$val.'" ';
- }
- $chaineTemp .= '/>';
- $this -> formBuffer['elements'][$cpt] = $chaineTemp;
- }
-
- // ouvrir un fieldset
- public function openFieldset ($arrArgs = array ()) {
- $cpt = count ($this -> formElementArr);
- $this -> formElementArr[$cpt]['fieldset'] = array ();
- $arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> fieldsetArr);
- foreach ($arrTemp as $clef => $val) {
- if (array_key_exists ($clef, $arrArgs)) {
- $this -> formElementArr[$cpt]['fieldset'][$clef] = $arrArgs[$clef];
- }
- }
- $chaineTemp = '<fieldset ';
- foreach ($this -> formElementArr[$cpt]['fieldset'] as $clef => $val) {
- $chaineTemp .= $clef.'="'.$val.'" ';
- }
- $chaineTemp .= '>';
- $this -> formBuffer['elements'][$cpt] = $chaineTemp;
- }
-
- // fermer un fieldset
- public function closeFieldset () {
- $cpt = count ($this -> formElementArr);
- $this -> formElementArr[$cpt]['/fieldset'] = array ();
- $chaineTemp = '</fieldset>';
- $this -> formBuffer['elements'][$cpt] = $chaineTemp;
- }
-
- // ajouter une légende
- public function addLegend ($legend, $arrArgs = array ()) {
- $cpt = count ($this -> formElementArr);
- $this -> formElementArr[$cpt]['legend']['innerHTML'] = $legend;
- $arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> legendArr);
- foreach ($arrTemp as $clef => $val) {
- if (array_key_exists ($clef, $arrArgs)) {
- $this -> formElementArr[$cpt]['legend'][$clef] = $arrArgs[$clef];
- }
- }
- $chaineTemp = '<legend ';
- foreach ($this -> formElementArr[$cpt]['legend'] as $clef => $val) {
- if ($clef !== 'innerHTML') {
- $chaineTemp .= $clef.'="'.$val.'" ';
- }
- }
- $chaineTemp .= '>'.$legend.'</legend>';
- $this -> formBuffer['elements'][$cpt] = $chaineTemp;
- }
-
- // ouvrir une balise p
- public function openP ($arrArgs = array ()) {
- $cpt = count ($this -> formElementArr);
- $this -> formElementArr[$cpt]['p'] = array ();
- $arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> pArr);
- foreach ($arrTemp as $clef => $val) {
- if (array_key_exists ($clef, $arrArgs)) {
- $this -> formElementArr[$cpt]['p'][$clef] = $arrArgs[$clef];
- }
- }
- $chaineTemp = '<p ';
- foreach ($this -> formElementArr[$cpt]['p'] as $clef => $val) {
- $chaineTemp .= $clef.'="'.$val.'" ';
- }
- $chaineTemp .= '>';
- $this -> formBuffer['elements'][$cpt] = $chaineTemp;
- }
-
- // fermer une balise p
- public function closeP () {
- $cpt = count ($this -> formElementArr);
- $this -> formElementArr[$cpt]['/p'] = array ();
- $chaineTemp = '</p>';
- $this -> formBuffer['elements'][$cpt] = $chaineTemp;
- }
-
- // ajouter un label
- public function addLabel ($label, $arrArgs = array ()) {
- $cpt = count ($this -> formElementArr);
- $this -> formElementArr[$cpt]['label']['innerHTML'] = $label;
- $arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> labelArr);
- foreach ($arrTemp as $clef => $val) {
- if (array_key_exists ($clef, $arrArgs)) {
- $this -> formElementArr[$cpt]['label'][$clef] = $arrArgs[$clef];
- }
- }
- $chaineTemp = '<label ';
- foreach ($this -> formElementArr[$cpt]['label'] as $clef => $val) {
- if ($clef !== 'innerHTML') {
- $chaineTemp .= $clef.'="'.$val.'" ';
- }
- }
- $chaineTemp .= '>'.$label.'</label>';
- $this -> formBuffer['elements'][$cpt] = $chaineTemp;
- }
-
- // ajouter un textarea
- public function addTextarea ($txt, $arrArgs = array ()) {
- $cpt = count ($this -> formElementArr);
- $this -> formElementArr[$cpt]['textarea']['innerHTML'] = $txt;
- $arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> textareaArr);
- foreach ($arrTemp as $clef => $val) {
- if (array_key_exists ($clef, $arrArgs)) {
- $this -> formElementArr[$cpt]['textarea'][$clef] = $arrArgs[$clef];
- }
- }
- $chaineTemp = '<textarea ';
- foreach ($this -> formElementArr[$cpt]['textarea'] as $clef => $val) {
- if ($clef !== 'innerHTML') {
- $chaineTemp .= $clef.'="'.$val.'" ';
- }
- }
- $chaineTemp .= '>'.$txt.'</textarea>';
- $this -> formBuffer['elements'][$cpt] = $chaineTemp;
- }
-
- // ouvrir un select
- public function openSelect ($arrArgs = array ()) {
- $cpt = count ($this -> formElementArr);
- $this -> formElementArr[$cpt]['select'] = array ();
- $arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> selectArr);
- foreach ($arrTemp as $clef => $val) {
- if (array_key_exists ($clef, $arrArgs)) {
- $this -> formElementArr[$cpt]['select'][$clef] = $arrArgs[$clef];
- }
- }
- $chaineTemp = '<select ';
- foreach ($this -> formElementArr[$cpt]['select'] as $clef => $val) {
- $chaineTemp .= $clef.'="'.$val.'" ';
- }
- $chaineTemp .= '>';
- $this -> formBuffer['elements'][$cpt] = $chaineTemp;
- }
-
- // fermer un select
- public function closeSelect () {
- $cpt = count ($this -> formElementArr);
- $this -> formElementArr[$cpt]['/select'] = array ();
- $chaineTemp = '</select>';
- $this -> formBuffer['elements'][$cpt] = $chaineTemp;
- }
-
- // ajouter une option
- public function addOption ($txt, $arrArgs = array ()) {
- $cpt = count ($this -> formElementArr);
- $this -> formElementArr[$cpt]['option']['innerHTML'] = $txt;
- $arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> optionArr);
- foreach ($arrTemp as $clef => $val) {
- if (array_key_exists ($clef, $arrArgs)) {
- $this -> formElementArr[$cpt]['option'][$clef] = $arrArgs[$clef];
- }
- }
- $chaineTemp = '<option ';
- foreach ($this -> formElementArr[$cpt]['option'] as $clef => $val) {
- if ($clef !== 'innerHTML') {
- $chaineTemp .= $clef.'="'.$val.'" ';
- }
- }
- $chaineTemp .= '>'.$txt.'</option>';
- $this -> formBuffer['elements'][$cpt] = $chaineTemp;
- }
-
- // ouvrir un optgroup
- public function openOptgroup ($label, $arrArgs = array ()) {
- $cpt = count ($this -> formElementArr);
- $this -> formElementArr[$cpt]['optgroup']['label'] = $label;
- $arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> optgroupArr);
- foreach ($arrTemp as $clef => $val) {
- if (array_key_exists ($clef, $arrArgs)) {
- $this -> formElementArr[$cpt]['select'][$clef] = $arrArgs[$clef];
- }
- }
- $chaineTemp = '<optgroup ';
- foreach ($this -> formElementArr[$cpt]['optgroup'] as $clef => $val) {
- $chaineTemp .= $clef.'="'.$val.'" ';
- }
- $chaineTemp .= '>';
- $this -> formBuffer['elements'][$cpt] = $chaineTemp;
- }
-
- // fermer un optgroup
- public function closeOptgroup () {
- $cpt = count ($this -> formElementArr);
- $this -> formElementArr[$cpt]['/optgroup'] = array ();
- $chaineTemp = '</optgroup>';
- $this -> formBuffer['elements'][$cpt] = $chaineTemp;
- }
-
- // ajouter n'importe quoi
- public function addAnything ($any) {
- $cpt = count ($this -> formElementArr);
- $this -> formBuffer['anything'][$cpt] = $any;
- }
-
- // méthode magique utilisée pour afficher effectivement le formulaire défini
- public function __toString () {
- $chaineTemp = '';
- if (isset ($this -> formBuffer['open']) && isset ($this -> formBuffer['close'])) {
- $chaineTemp = $this -> formBuffer['open'];
- if (isset ($this -> formBuffer['elements']) && !empty ($this -> formBuffer['elements'])) {
- foreach ($this -> formBuffer['elements'] as $clef => $val) {
- if (isset ($this -> formBuffer['anything'][$clef])) {
- $chaineTemp .= $this -> formBuffer['anything'][$clef];
- }
- $chaineTemp .= $val;
- }
- }
- $chaineTemp .= $this -> formBuffer['close'];
- }
- return $chaineTemp;
- }
-
- // méthode pour libérer les ressources et créer un nouveau formulaire (tout formulaire réé auparavant et non affiché sera perdu)
- public function freeForm () {
- $this -> formBuffer = array ();
- $this -> formElementArr = array ();
- $this -> formAttributeArr = array ();
- }
-
- // destructeur (en attendant mieux...)
- public function __destruct () {
- unset ($this);
- }
-
- /***************************
- ***METHODS FOR DEBUGGING***
- ***************************/
-
- // méthode affichant tous les éléments que contient le formulaire
- public function showElems () {
- $chaineTemp = '';
- foreach ($this -> formElementArr as $clef => $val) {
- foreach ($val as $elem => $attrArr) {
- if (strpos ($elem, '/') !== false) {
- $chaineTemp .= '<ul><li style="color: blue;">end '.substr ($elem, 1, strlen ($elem)).'</li></ul>';
- }
- else {
- $chaineTemp .= '<ul><li style="color: blue;">'.$elem.'</li><ul>';
- foreach ($attrArr as $attr => $value) {
- $chaineTemp .= '<li style="color: red;">'.$attr.' = <span style="color: green; font-style: italic;">'.$value.'</span></li>';
- }
- $chaineTemp .= '</ul></ul>';
- }
- }
- }
- return $chaineTemp;
- }
-
- // méthode coomptant les éléments que contient le formulaire : total global, et total par élément
- public function countElems () {
- foreach ($this -> formElementArr as $clef => $val) {
- foreach ($val as $elem => $attrArr) {
- if (strpos ($elem, '/') === false) {
- $arrTemp[] = $elem;
- }
- }
- }
- $cptElem = count ($arrTemp);
- $arrEachElem = array_count_values ($arrTemp);
- $chaineTemp = '<span style="color: black; font-weight: bold;">Total éléments : <span style="color: red;">'.$cptElem.'</span><br />dont : </span><br />';
- ksort ($arrEachElem, SORT_STRING);
- foreach ($arrEachElem as $elem => $nbr) {
- $chaineTemp .= '<span style="color: blue; margin-left: 20px;">'.$elem.' : </span><span style="color: red;">'.$nbr.'</span><br />';
- }
- return $chaineTemp;
- }
-
- }
-
- // on instancie notre objet
- $form = new form ();
- // on crée un 1er formulaire
-
- $form -> openForm (array ('action' => '?', 'id' => 'MyForm'));
- $form -> openFieldset (array ('style' => 'border:1px dotted red; width: 300px;'));
- $form -> addLegend ('test');
- $form -> addInput ('text', array ('id' => 'MyText', 'value' => 'ok', 'test' => 'test'));
- $form -> addLabel ('label', array ('for' => 'MyText', 'style' => 'margin: 5px;'));
- $form -> addAnything ('<br /><br />');
- $form -> addInput ('button', array ('id' => 'MyButton', 'value' => 'click!', 'test' => 'test'));
- $form -> closeFieldset ();
- $form -> closeForm ();
-
- echo '<div style="border: 1px solid darkgrey; text-align: center; width: 310px;">';
- // on l'affiche
- echo $form;
- echo '</div>';
-
- // on compte et affiche ses éléments (debugging only)
- echo $form -> showElems ();
- echo $form -> countElems ();
-
- // on libère les ressources pour pouvoir créer un 2d formulaire
- $form -> freeForm ();
-
- // on réinitialise un nouveau formulaire
- // on ouvre effectivement le nouveau formulaire
- $form -> openForm (array ('action' => '?', 'id' => 'MyForm2'));
- $form -> openFieldset (array ('style' => 'border:1px dotted blue; width: 300px;'));
- $form -> addLegend ('test 2');
- $form -> addInput ('text', array ('id' => 'MyText2', 'value' => 'yep', 'test' => 'test'));
- $form -> addInput ('checkbox', array ('id' => 'MyCheck', 'value' => '1', 'test' => 'test'));
- $form -> addLabel ('Checkbox', array ('for' => 'MyCheck'));
- $form -> addTextarea ('mon texte', array ('cols' => 20, 'rows' => 10));
- $form -> openSelect ();
- $form -> openOptgroup ('label options 1');
- $form -> addOption ('1');
- $form -> closeOptgroup ();
- $form -> openOptgroup ('label options 2');
- $form -> addOption ('2');
- $form -> addOption ('2_2');
- $form -> closeOptgroup ();
- $form -> closeSelect ();
- $form -> closeFieldset ();
- $form -> closeForm ();
-
- echo '<div style="border: 1px solid orange; text-align: center; width: 310px;">';
- echo $form;
- echo '</div>';
-
- echo $form -> showElems ();
- echo $form -> countElems ();
-
- $form -> freeForm ();
-
- ?>
<?php
class form {
// propriétés privées : tous les éléments et attributs utilisables (certaines valeurs sont entrées par défaut)
private $eventArr = array ('onfocus' => '',
'onblur' => '',
'onselect' => '',
'onchange' => '',
'onclick' => '',
'ondblclick' => '',
'onmousedown' => '',
'onmouseup' => '',
'onmouseover' => '',
'onmousemove' => '',
'onmouseout' => '',
'onkeypress' => '',
'onkeydown' => '',
'onkeyup' => '');
private $commonArr = array ('id' => '',
'class' => '',
'title' => '',
'style' => '',
'dir' => '',
'lang' => '',
'xml:lang' => '');
private $formArr = array (
'method' => 'post',
'action' => '',
'id' => 'mainForm',
'enctype' => 'application/x-www-form-urlencoded',
'accept' => '',
'onsubmit' => '',
'onreset' => '',
'accept-charset' => 'unknown',
'style' => ''
);
private $inputArr = array ('text' => array ('value' => '',
'name' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'readonly' => '',
'disabled' => '',
'width' => '',
'maxlength' => '',
'size ' => ''),
'button' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => ''),
'hidden' => array ('name' => '',
'value' => '',
'alt' => '',
'disabled' => '',
'size ' => ''),
'password' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'readonly' => '',
'disabled' => '',
'width' => '',
'maxlength' => '',
'size ' => ''),
'submit' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => ''),
'checkbox' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => '',
'checked' => ''),
'radio' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => '',
'checked' => '',
'title' => ''),
'reset' => array ('name' => '',
'class' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => '',
'title' => ''),
'file' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => '',
'accept' => '',
'size ' => ''),
'image' => array ('name' => '',
'value' => '',
'alt' => '',
'tabindex' => '',
'accesskey' => '',
'disabled' => '',
'src' => '',
'usemap' => '',
'ismap' => '')
);
private $fieldsetArr = array ();
private $pArr = array ();
private $legendArr = array ();
private $labelArr = array ('for' => '');
private $textareaArr = array ('rows' => '',
'cols' => '',
'disabled' => '',
'readonly' => '',
'accesskey' => '',
'tabindex' => '',
'name' => '');
private $selectArr = array ('disabled' => '',
'multiple' => '',
'size' => '',
'name' => '');
private $optionArr = array ('disabled' => '',
'label' => '',
'selected' => '',
'value' => '');
private $optgroupArr = array ('disabled' => '');
private $formBuffer = array ();
private $formElementArr = array ();
private $formAttributeArr = array ();
//Constructeur
public function __construct () {
}
// débuter effectivement le formulaire
public function openForm ($arrArgs = array ()) {
foreach ($this -> formArr as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formAttributeArr[$clef] = $arrArgs[$clef];
}
else if (!empty ($val)) {
$this -> formAttributeArr[$clef] = $val;
}
}
$this -> formBuffer['open'] = '<form ';
foreach ($this -> formAttributeArr as $clef => $val) {
$this -> formBuffer['open'] .= $clef.'="'.$val.'" ';
}
$this -> formBuffer['open'] .= '>';
}
// fermer le formulaire
public function closeForm () {
$this -> formBuffer['close'] = '</form>';
}
// ajouter un type input
public function addInput ($elem, $arrArgs = array ()) {
if (!array_key_exists ($elem, $this -> inputArr)) {
throw new Exception ($elem.' n\'est pas un élément valide');
}
if (!array_key_exists ('name', $arrArgs) && $elem !== 'submit' && $elem !== 'reset') {
$arrArgs['name'] = 'default';
}
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt][$elem] = array ();
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> inputArr[$elem]);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt][$elem][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<input type="'.$elem.'" ';
foreach ($this -> formElementArr[$cpt][$elem] as $clef => $val) {
$chaineTemp .= $clef.'="'.$val.'" ';
}
$chaineTemp .= '/>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}
// ouvrir un fieldset
public function openFieldset ($arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['fieldset'] = array ();
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> fieldsetArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['fieldset'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<fieldset ';
foreach ($this -> formElementArr[$cpt]['fieldset'] as $clef => $val) {
$chaineTemp .= $clef.'="'.$val.'" ';
}
$chaineTemp .= '>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}
// fermer un fieldset
public function closeFieldset () {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['/fieldset'] = array ();
$chaineTemp = '</fieldset>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}
// ajouter une légende
public function addLegend ($legend, $arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['legend']['innerHTML'] = $legend;
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> legendArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['legend'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<legend ';
foreach ($this -> formElementArr[$cpt]['legend'] as $clef => $val) {
if ($clef !== 'innerHTML') {
$chaineTemp .= $clef.'="'.$val.'" ';
}
}
$chaineTemp .= '>'.$legend.'</legend>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}
// ouvrir une balise p
public function openP ($arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['p'] = array ();
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> pArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['p'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<p ';
foreach ($this -> formElementArr[$cpt]['p'] as $clef => $val) {
$chaineTemp .= $clef.'="'.$val.'" ';
}
$chaineTemp .= '>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}
// fermer une balise p
public function closeP () {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['/p'] = array ();
$chaineTemp = '</p>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}
// ajouter un label
public function addLabel ($label, $arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['label']['innerHTML'] = $label;
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> labelArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['label'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<label ';
foreach ($this -> formElementArr[$cpt]['label'] as $clef => $val) {
if ($clef !== 'innerHTML') {
$chaineTemp .= $clef.'="'.$val.'" ';
}
}
$chaineTemp .= '>'.$label.'</label>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}
// ajouter un textarea
public function addTextarea ($txt, $arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['textarea']['innerHTML'] = $txt;
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> textareaArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['textarea'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<textarea ';
foreach ($this -> formElementArr[$cpt]['textarea'] as $clef => $val) {
if ($clef !== 'innerHTML') {
$chaineTemp .= $clef.'="'.$val.'" ';
}
}
$chaineTemp .= '>'.$txt.'</textarea>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}
// ouvrir un select
public function openSelect ($arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['select'] = array ();
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> selectArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['select'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<select ';
foreach ($this -> formElementArr[$cpt]['select'] as $clef => $val) {
$chaineTemp .= $clef.'="'.$val.'" ';
}
$chaineTemp .= '>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}
// fermer un select
public function closeSelect () {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['/select'] = array ();
$chaineTemp = '</select>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}
// ajouter une option
public function addOption ($txt, $arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['option']['innerHTML'] = $txt;
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> optionArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['option'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<option ';
foreach ($this -> formElementArr[$cpt]['option'] as $clef => $val) {
if ($clef !== 'innerHTML') {
$chaineTemp .= $clef.'="'.$val.'" ';
}
}
$chaineTemp .= '>'.$txt.'</option>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}
// ouvrir un optgroup
public function openOptgroup ($label, $arrArgs = array ()) {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['optgroup']['label'] = $label;
$arrTemp = array_merge ($this -> eventArr, $this -> commonArr, $this -> optgroupArr);
foreach ($arrTemp as $clef => $val) {
if (array_key_exists ($clef, $arrArgs)) {
$this -> formElementArr[$cpt]['select'][$clef] = $arrArgs[$clef];
}
}
$chaineTemp = '<optgroup ';
foreach ($this -> formElementArr[$cpt]['optgroup'] as $clef => $val) {
$chaineTemp .= $clef.'="'.$val.'" ';
}
$chaineTemp .= '>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}
// fermer un optgroup
public function closeOptgroup () {
$cpt = count ($this -> formElementArr);
$this -> formElementArr[$cpt]['/optgroup'] = array ();
$chaineTemp = '</optgroup>';
$this -> formBuffer['elements'][$cpt] = $chaineTemp;
}
// ajouter n'importe quoi
public function addAnything ($any) {
$cpt = count ($this -> formElementArr);
$this -> formBuffer['anything'][$cpt] = $any;
}
// méthode magique utilisée pour afficher effectivement le formulaire défini
public function __toString () {
$chaineTemp = '';
if (isset ($this -> formBuffer['open']) && isset ($this -> formBuffer['close'])) {
$chaineTemp = $this -> formBuffer['open'];
if (isset ($this -> formBuffer['elements']) && !empty ($this -> formBuffer['elements'])) {
foreach ($this -> formBuffer['elements'] as $clef => $val) {
if (isset ($this -> formBuffer['anything'][$clef])) {
$chaineTemp .= $this -> formBuffer['anything'][$clef];
}
$chaineTemp .= $val;
}
}
$chaineTemp .= $this -> formBuffer['close'];
}
return $chaineTemp;
}
// méthode pour libérer les ressources et créer un nouveau formulaire (tout formulaire réé auparavant et non affiché sera perdu)
public function freeForm () {
$this -> formBuffer = array ();
$this -> formElementArr = array ();
$this -> formAttributeArr = array ();
}
// destructeur (en attendant mieux...)
public function __destruct () {
unset ($this);
}
/***************************
***METHODS FOR DEBUGGING***
***************************/
// méthode affichant tous les éléments que contient le formulaire
public function showElems () {
$chaineTemp = '';
foreach ($this -> formElementArr as $clef => $val) {
foreach ($val as $elem => $attrArr) {
if (strpos ($elem, '/') !== false) {
$chaineTemp .= '<ul><li style="color: blue;">end '.substr ($elem, 1, strlen ($elem)).'</li></ul>';
}
else {
$chaineTemp .= '<ul><li style="color: blue;">'.$elem.'</li><ul>';
foreach ($attrArr as $attr => $value) {
$chaineTemp .= '<li style="color: red;">'.$attr.' = <span style="color: green; font-style: italic;">'.$value.'</span></li>';
}
$chaineTemp .= '</ul></ul>';
}
}
}
return $chaineTemp;
}
// méthode coomptant les éléments que contient le formulaire : total global, et total par élément
public function countElems () {
foreach ($this -> formElementArr as $clef => $val) {
foreach ($val as $elem => $attrArr) {
if (strpos ($elem, '/') === false) {
$arrTemp[] = $elem;
}
}
}
$cptElem = count ($arrTemp);
$arrEachElem = array_count_values ($arrTemp);
$chaineTemp = '<span style="color: black; font-weight: bold;">Total éléments : <span style="color: red;">'.$cptElem.'</span><br />dont : </span><br />';
ksort ($arrEachElem, SORT_STRING);
foreach ($arrEachElem as $elem => $nbr) {
$chaineTemp .= '<span style="color: blue; margin-left: 20px;">'.$elem.' : </span><span style="color: red;">'.$nbr.'</span><br />';
}
return $chaineTemp;
}
}
// on instancie notre objet
$form = new form ();
// on crée un 1er formulaire
$form -> openForm (array ('action' => '?', 'id' => 'MyForm'));
$form -> openFieldset (array ('style' => 'border:1px dotted red; width: 300px;'));
$form -> addLegend ('test');
$form -> addInput ('text', array ('id' => 'MyText', 'value' => 'ok', 'test' => 'test'));
$form -> addLabel ('label', array ('for' => 'MyText', 'style' => 'margin: 5px;'));
$form -> addAnything ('<br /><br />');
$form -> addInput ('button', array ('id' => 'MyButton', 'value' => 'click!', 'test' => 'test'));
$form -> closeFieldset ();
$form -> closeForm ();
echo '<div style="border: 1px solid darkgrey; text-align: center; width: 310px;">';
// on l'affiche
echo $form;
echo '</div>';
// on compte et affiche ses éléments (debugging only)
echo $form -> showElems ();
echo $form -> countElems ();
// on libère les ressources pour pouvoir créer un 2d formulaire
$form -> freeForm ();
// on réinitialise un nouveau formulaire
// on ouvre effectivement le nouveau formulaire
$form -> openForm (array ('action' => '?', 'id' => 'MyForm2'));
$form -> openFieldset (array ('style' => 'border:1px dotted blue; width: 300px;'));
$form -> addLegend ('test 2');
$form -> addInput ('text', array ('id' => 'MyText2', 'value' => 'yep', 'test' => 'test'));
$form -> addInput ('checkbox', array ('id' => 'MyCheck', 'value' => '1', 'test' => 'test'));
$form -> addLabel ('Checkbox', array ('for' => 'MyCheck'));
$form -> addTextarea ('mon texte', array ('cols' => 20, 'rows' => 10));
$form -> openSelect ();
$form -> openOptgroup ('label options 1');
$form -> addOption ('1');
$form -> closeOptgroup ();
$form -> openOptgroup ('label options 2');
$form -> addOption ('2');
$form -> addOption ('2_2');
$form -> closeOptgroup ();
$form -> closeSelect ();
$form -> closeFieldset ();
$form -> closeForm ();
echo '<div style="border: 1px solid orange; text-align: center; width: 310px;">';
echo $form;
echo '</div>';
echo $form -> showElems ();
echo $form -> countElems ();
$form -> freeForm ();
?>
Conclusion
C'est juste un début; je m'attèlerai à des ajouts éventuellement si le besoin d'en fait ressentir.
Historique
- 08 décembre 2005 13:50:08 :
- Petite correction sur $val !== '' ...
- 08 décembre 2005 14:00:52 :
- Rien, rien... ;-)
- 09 décembre 2005 14:51:35 :
- J'avais oublié d'autoriser l'attribut 'name' pour les select et les textarea...génant... !
- 28 janvier 2006 10:57:23 :
- Ajout d'une méthode pour inclure des balises <p></p>. Fonctionne comme les fieldset : on ouvre avec openP () et on ferme avec closeP ().
Ce, pour être plus conforme XHTML.
- 02 février 2006 12:26:04 :
- Suppression de l'attribut innerHTML dans le code html (mais je le conserve dans la classe, pour la méthode showElems () )
- 01 mars 2006 13:52:00 :
- Corrections diverses (bugs remontés par Stepibou, merci :-) )
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
un Form dans un Form [ par stailer ]
Salut tout le monde !Donc voila mon problème :J'ai un formulaire qui contient notamment un champ et une liste déroulante.Lorsque je valide 1 de ces ch
2 boutons submit dans le meme form? [ par LaurentKOogar ]
Bonjour et bon début de week end à tous,ptit problème==>ya 1 formulaire avec 1 champt text et dans ce formulaire il faudrait 2 boutons submit1 bout
pb avec formulaire [ par billy67000 ]
Salut, dans mon formulaire je recupere une valeur d'une base de donnée (MAX(id)): $val = .... mon form est ici <form name="frm_category" act
Envoyer un form par mail ! ! ! [ par laubro ]
Bonjour,j'ai un formulaire que je voudrais me faire envoyer "par mail", celui-ci contien des champsclassiques : nom prenom adresse......+ 1 case texta
Problème inclusion form dans un autre form [ par 2swfan ]
Bonjour tout le monde !Voila, je dois faire un formulaire d'ajout de cartes géographiques. Dans ce formulaire, l'utilisateur saisit le nom,
Problème de tests sur l'envoi d'un formulaire [ par seabird ]
Salut a tous , J'ai un petit soucis que je n'arrive pas à résoudre. En effet j'envois un formulaire par mail qui contient plusieurs champs e
Return pour une Classe [ par mandark ]
Bonjour à tous et encore merci à tous de l'aide que vous offrez ici (:Voilà j'aimerai savoir comment gérer les valeurs de retour d
[PHP5] Etendre une classe prédéfinie [ par ZuGbEn ]
Bonjour !Je débute plus ou moins dans la programmation orientée objet, et je souhaite faire une classe qui, à partir d'un DomNode existant, ajoute des
traitement de plusieurs formulaires sur une seule page php [ par arnold002 ]
Bonjour à tous,J'ai un formulaire d'inscription relativement long.Actuellement, l'utilisateur remplit ce formulaire et le valide. Le traitement php se
redirection [ par kenny18 ]
salut,j'ai un gros soucis de redirection.j'ai ma page formulaire.php qui contient un formulaire. Lorsque je clique sur "envoyer", cela va appeler des
|
Derniers Blogs
GESTION D'EXCEPTION AVEC LES TASKSGESTION D'EXCEPTION AVEC LES TASKS par richardc
Nous avons vu dans un précédent article comment utiliser Task pour effectuer des opérations dans un autre thread.
Malheureusement, comme tout le monde n'est pas parfait, il se peut que cette exécution se passe mal et qu'une exception se produise.
La...
Cliquez pour lire la suite de l'article par richardc DéMARRONS AVEC LES TASKSDéMARRONS AVEC LES TASKS par richardc
Que vous le vouliez ou non, le développement multi-tâche est maintenant une obligation pour toute nouvelle application. Il est donc vital d'en comprendre les mécanismes et de s'y mettre le plus tôt possible.
En attendant le .NET Framework 4.5 avec le...
Cliquez pour lire la suite de l'article par richardc SLIDE & DéMO TECHDAYS 2012 - FAST & FURIOUS XAML APPSSLIDE & DéMO TECHDAYS 2012 - FAST & FURIOUS XAML APPS par Vko
Retrouvez les slides et les démo de ma session Fast & Furious XAML Apps. A ceux qui se posent la question : "est-ce que le code de la DataGrid est disponible?", je vous répondrais "pas encore". Je vais mettre en place un projet codeplex pour part...
Cliquez pour lire la suite de l'article par Vko XNA IS DEAD!XNA IS DEAD! par richardc
Depuis la semaine dernière (et grâce aux TechDays 2012), je me penche activement sur la nouvelle version de Windows, aka Windows 8. Vous me direz, il était temps puisque la première preview date de Septembre dernier.
OK. Remarquez, on n'en est qu'aux...
Cliquez pour lire la suite de l'article par richardc TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 !TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 ! par ROMELARD Fabrice
Speakers: Fabrice Meillon et Stanislas Quastana Cette session est basée entièrement sur celle donnée lors de la BUILD cet hiver. Il n'y a pas d'ajout d'information en rapport avec cet évènement passé. Windows 8 Server sera intégralem...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Forum
CHAMPS TIMECHAMPS TIME par vargas
Cliquez pour lire la suite par vargas
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|