par
rimie1 » 15 avr. 2011, 06:31
un essai mediocre, pas encore termine:
<?php
class formulaire {
// Toutes les propriétés du formulaire
private $formMethod; // array (POST et GET)
private $method; // POST et GET
private $action;
private $name;
private $value;
private $type; // text, password, button, submit, radio, checkbox
private $id;
private $class;
private $size;
private $maxlength;
private $cols;
private $rows;
//private $inputType;
//private $error;
//private $form;
//private $afterinput;
######## On construit le formulaire:
public final function __construct($name, $method='', $action='') { // aucun champ n'est obligatoire
// le nom du formulaire:
$this->name = $name;
// la methode
# on liste les differentes methodes
$this->formMethod = array('POST','GET');
// on supprime l'espace
$method = trim($method);
// si la method est vide on met POST par defaut sinon on la laisse
$method = (empty($method)) ? 'POST' : $method;
// si la method remplie ne contient pas le tableau formMethod (post, get) on met a defaut post
$method = (!in_array($method, $this->formMethod)) ? 'POST' : $method;
// on retourne la method
$this->method = $method;
//l'action
$action = (empty($action)) ? $_SERVER['PHP_SELF'] : $action;
$this->action = $action;
// l'entete du formulaire
echo '<form name="'.$name.'" method="'.$method.'" action="'.$action.'" >';
}
//public final function addInput($name, $type='', $checked='', $size='', $maxlength=''){
public final function addInput($name, $type='', $checked=''){
//echo $type;
$value = (empty($value)) ? $name : $value;
$id = (empty($id)) ? $name : $id;
$class = (empty($class)) ? $name : $class;
$size = (empty($rows)) ? 20 : $size;
$maxlength = (empty($maxlength)) ? 50 : $maxlength;
$this->name = $name; // nom
$this->id = $id; // id
$this->class = $class; // class
$this->cols = $size; // size
$this->rows = $maxlength; // maxi
switch($type)
{
case 'password':
echo '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" size="'.$size.'" maxlength="' .$maxlength.'" id="'.$id.'" class="'.$class.'" />';
break;
case 'submit':
echo '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" id="'.$id.'" class="'.$class.'" />';
break;
case 'button':
echo '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" id="'.$id.'" class="'.$class.'" />';
break;
case 'radio':
if($checked)
{
echo '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" id="'.$id.'" class="'.$class.'" '.$checked.' />';
}
else
{
echo '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" id="'.$id.'" class="'.$class.'" />';
}
break;
case 'checkbox':
if($checked)
{
echo '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" id="'.$id.'" class="'.$class.'" '.$checked.' />';
}
else
{
echo '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" id="'.$id.'" class="'.$class.'" />';
}
break;
// par defaut un text
default:
echo '<input type="text" name="'.$name.'" value="'.$value.'" size="'.$size.'" maxlength="'.$maxlength.'" id="'.$id.'" class="'.$class.'" />';
}
$this->type = $type;
//$this->inputType = array('text', 'password', 'submit', 'button', 'radio','checkbox');
// si le type est vide on met text par defaut sinon on la laisse
//$type = (empty($text)) ? 'text' : $text;
// si le type ne contient pas le tableau inputType (text, ...) on le met a defaut text
//$type = (!in_array($type, $this->inputType)) ? 'text' : $type;
// on retourne le type
} // end addInput function
#Textarea
public final function addTextarea($name, $cols='', $rows='', $fillTextarea='', $optionsTextarea=''){
$id = (empty($id)) ? $name : $id;
$class = (empty($class)) ? $name : $class;
$fillTextarea = (empty($fillTextarea)) ? 'Du text' : $fillTextarea;
$cols = (empty($cols)) ? 40 : $cols;
$rows = (empty($rows)) ? 5 : $rows;
$this->name = $name; // nom
$this->id = $id; // id
$this->class = $class; // class
$this->cols = $cols; // cols
$this->rows = $rows; // rows
if($optionsTextarea)
{
echo '<textarea name="'.$name.'" cols="'.$cols.'" rows="'.$rows.'" '.$optionsTextarea.'>'.$fillTextarea.'</textarea>';
}
else
{
echo '<textarea name="'.$name.'" cols="'.$cols.'" rows="'.$rows.'">'.$fillTextarea.'</textarea>';
}
} // end textarea
#StartSelect
public final function addStartSelect($name=''){
$id = (empty($id)) ? $name : $id;
$class = (empty($class)) ? $name : $class;
//$size = (empty($size)) ? '' : $size;
$this->name = $name; // nom
$this->id = $id; // id
$this->class = $class; // class
//$this->size = $size; // size
//$this->rows = $rows; // rows
echo '<select name="'.$name.'">';
} // end StartSelect
#Options Select
public final function addOptionsSelect($fillOption, $value='', $optionOptions=''){
$id = (empty($id)) ? $name : $id;
$class = (empty($class)) ? $name : $class;
$value = (empty($value)) ? 0 : $value;
$fillOption = (empty($fillOption)) ? 'Select from list' : $fillOption;
$this->name = $name; // nom
$this->id = $id; // id
$this->class = $class; // class
$this->value = $value; // valeur option
//$this->rows = $rows; // rows
if($optionOptions)
{
echo '<option value="'.$value.'" DISABLED>'.$fillOption.'</option>';
}
else
{
echo '<option value="'.$value.'">'.$fillOption.'</option>';
}
} // end Options Select
#EndSelect
public final function addEndSelect(){
echo '</select>';
} // end EndSelect
// separation des champs
public function separation()
{
echo "<br />";
}
// terminer le Form
public function terminer()
{
echo "</form>";
}
}// end class formulaire
$frm1 = new formulaire('monForm1', '', ''); // imprimer l'entête du formulaire (name, method, action)
$frm1->addInput('nom', 'text'); // champ text
//$frm1->addInput('nom', 'text', '10', '80'); // champ text avec modif
$frm1->separation();
$frm1->addInput('nom', 'checkbox', 'Checked="checked"'); // checkbox avec checked
$frm1->addInput('nom', 'checkbox'); // checkbox sans checked
$frm1->separation();
$frm1->addInput('nom', 'radio', 'Checked="checked"'); // radio avec checked
$frm1->addInput('nom', 'radio'); // radio sans checked
$frm1->separation();
$frm1->addTextarea('nom', '', ''); // textarea sans text
$frm1->separation();
$frm1->addTextarea('nom', '20', '3', 'Test ici'); // textarea avec du text
$frm1->separation();
$frm1->addTextarea('nom', '35', '3', 'textarea avec readonly', 'READONLY'); // textarea avec READONLY
$frm1->separation();
$frm1->addStartSelect('select1');
$frm1->addOptionsSelect('','','DISABLED');
$frm1->addOptionsSelect('valeur2', 'option4');
$frm1->addEndSelect();
$frm1->separation();
$frm1->terminer();
?>
un essai mediocre, pas encore termine:
[php]
<?php
class formulaire {
// Toutes les propriétés du formulaire
private $formMethod; // array (POST et GET)
private $method; // POST et GET
private $action;
private $name;
private $value;
private $type; // text, password, button, submit, radio, checkbox
private $id;
private $class;
private $size;
private $maxlength;
private $cols;
private $rows;
//private $inputType;
//private $error;
//private $form;
//private $afterinput;
######## On construit le formulaire:
public final function __construct($name, $method='', $action='') { // aucun champ n'est obligatoire
// le nom du formulaire:
$this->name = $name;
// la methode
# on liste les differentes methodes
$this->formMethod = array('POST','GET');
// on supprime l'espace
$method = trim($method);
// si la method est vide on met POST par defaut sinon on la laisse
$method = (empty($method)) ? 'POST' : $method;
// si la method remplie ne contient pas le tableau formMethod (post, get) on met a defaut post
$method = (!in_array($method, $this->formMethod)) ? 'POST' : $method;
// on retourne la method
$this->method = $method;
//l'action
$action = (empty($action)) ? $_SERVER['PHP_SELF'] : $action;
$this->action = $action;
// l'entete du formulaire
echo '<form name="'.$name.'" method="'.$method.'" action="'.$action.'" >';
}
//public final function addInput($name, $type='', $checked='', $size='', $maxlength=''){
public final function addInput($name, $type='', $checked=''){
//echo $type;
$value = (empty($value)) ? $name : $value;
$id = (empty($id)) ? $name : $id;
$class = (empty($class)) ? $name : $class;
$size = (empty($rows)) ? 20 : $size;
$maxlength = (empty($maxlength)) ? 50 : $maxlength;
$this->name = $name; // nom
$this->id = $id; // id
$this->class = $class; // class
$this->cols = $size; // size
$this->rows = $maxlength; // maxi
switch($type)
{
case 'password':
echo '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" size="'.$size.'" maxlength="' .$maxlength.'" id="'.$id.'" class="'.$class.'" />';
break;
case 'submit':
echo '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" id="'.$id.'" class="'.$class.'" />';
break;
case 'button':
echo '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" id="'.$id.'" class="'.$class.'" />';
break;
case 'radio':
if($checked)
{
echo '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" id="'.$id.'" class="'.$class.'" '.$checked.' />';
}
else
{
echo '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" id="'.$id.'" class="'.$class.'" />';
}
break;
case 'checkbox':
if($checked)
{
echo '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" id="'.$id.'" class="'.$class.'" '.$checked.' />';
}
else
{
echo '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" id="'.$id.'" class="'.$class.'" />';
}
break;
// par defaut un text
default:
echo '<input type="text" name="'.$name.'" value="'.$value.'" size="'.$size.'" maxlength="'.$maxlength.'" id="'.$id.'" class="'.$class.'" />';
}
$this->type = $type;
//$this->inputType = array('text', 'password', 'submit', 'button', 'radio','checkbox');
// si le type est vide on met text par defaut sinon on la laisse
//$type = (empty($text)) ? 'text' : $text;
// si le type ne contient pas le tableau inputType (text, ...) on le met a defaut text
//$type = (!in_array($type, $this->inputType)) ? 'text' : $type;
// on retourne le type
} // end addInput function
#Textarea
public final function addTextarea($name, $cols='', $rows='', $fillTextarea='', $optionsTextarea=''){
$id = (empty($id)) ? $name : $id;
$class = (empty($class)) ? $name : $class;
$fillTextarea = (empty($fillTextarea)) ? 'Du text' : $fillTextarea;
$cols = (empty($cols)) ? 40 : $cols;
$rows = (empty($rows)) ? 5 : $rows;
$this->name = $name; // nom
$this->id = $id; // id
$this->class = $class; // class
$this->cols = $cols; // cols
$this->rows = $rows; // rows
if($optionsTextarea)
{
echo '<textarea name="'.$name.'" cols="'.$cols.'" rows="'.$rows.'" '.$optionsTextarea.'>'.$fillTextarea.'</textarea>';
}
else
{
echo '<textarea name="'.$name.'" cols="'.$cols.'" rows="'.$rows.'">'.$fillTextarea.'</textarea>';
}
} // end textarea
#StartSelect
public final function addStartSelect($name=''){
$id = (empty($id)) ? $name : $id;
$class = (empty($class)) ? $name : $class;
//$size = (empty($size)) ? '' : $size;
$this->name = $name; // nom
$this->id = $id; // id
$this->class = $class; // class
//$this->size = $size; // size
//$this->rows = $rows; // rows
echo '<select name="'.$name.'">';
} // end StartSelect
#Options Select
public final function addOptionsSelect($fillOption, $value='', $optionOptions=''){
$id = (empty($id)) ? $name : $id;
$class = (empty($class)) ? $name : $class;
$value = (empty($value)) ? 0 : $value;
$fillOption = (empty($fillOption)) ? 'Select from list' : $fillOption;
$this->name = $name; // nom
$this->id = $id; // id
$this->class = $class; // class
$this->value = $value; // valeur option
//$this->rows = $rows; // rows
if($optionOptions)
{
echo '<option value="'.$value.'" DISABLED>'.$fillOption.'</option>';
}
else
{
echo '<option value="'.$value.'">'.$fillOption.'</option>';
}
} // end Options Select
#EndSelect
public final function addEndSelect(){
echo '</select>';
} // end EndSelect
// separation des champs
public function separation()
{
echo "<br />";
}
// terminer le Form
public function terminer()
{
echo "</form>";
}
}// end class formulaire
$frm1 = new formulaire('monForm1', '', ''); // imprimer l'entête du formulaire (name, method, action)
$frm1->addInput('nom', 'text'); // champ text
//$frm1->addInput('nom', 'text', '10', '80'); // champ text avec modif
$frm1->separation();
$frm1->addInput('nom', 'checkbox', 'Checked="checked"'); // checkbox avec checked
$frm1->addInput('nom', 'checkbox'); // checkbox sans checked
$frm1->separation();
$frm1->addInput('nom', 'radio', 'Checked="checked"'); // radio avec checked
$frm1->addInput('nom', 'radio'); // radio sans checked
$frm1->separation();
$frm1->addTextarea('nom', '', ''); // textarea sans text
$frm1->separation();
$frm1->addTextarea('nom', '20', '3', 'Test ici'); // textarea avec du text
$frm1->separation();
$frm1->addTextarea('nom', '35', '3', 'textarea avec readonly', 'READONLY'); // textarea avec READONLY
$frm1->separation();
$frm1->addStartSelect('select1');
$frm1->addOptionsSelect('','','DISABLED');
$frm1->addOptionsSelect('valeur2', 'option4');
$frm1->addEndSelect();
$frm1->separation();
$frm1->terminer();
?>
[/php]