<html>
<body>
<form name="formulaire_general" method="post">
....
<?php include("page1.php"); ?>
....
<?php include("page2.php"); ?>
..... autres champs
</form>
</body>
</html>
voici le contenu des sous-formulaires, par exemple, page1.php :
<p>Nom * <input name="nom" value="" /></p>
<p>Prénom * <input name="prenom" value="" /></p>
... autres champs
etc ...<?php
//include("class.form.php");
class Form {
function name_field() {
echo '<input type="text" name="name" id="name" size="30" maxlength="30" />';
}
}
$frm = new Form;
$frm->name_field();
?>
<br />
<?php
class Formx {
function name_fieldx( $name_fieldx) {
echo $name_fieldx.' <input type="text" name="name" id="name" size="30" maxlength="30" />';
}
}
$frmx = new Formx;
$frmx->name_fieldx('Name: ');
$frmx->name_fieldx('Nom: ');
?>
<input type="text" name="name" id="name" size="30" maxlength="30" />
et lors de l'appel j'ajoute class='ma_class'
<?php
class Formx {
function name_fieldx( $name_fieldx) {
echo $name_fieldx.' <input type="text" name="name" id="name" size="30" maxlength="30" />';
}
}
$frmx = new Formx;
$frmx->name_fieldx('Name: ');
$frmx->name_fieldx('Nom: ');
?>
je peux ajouter d'autres attributs a input?<input type="text" name="name" id="name" size="30" maxlength="30" />
j'aimerais l'appeller et a chaque fois je voudrais effectuer des changements, par exemple, ajouter une class:<input type="text" name="name" id="name" size="30" maxlength="30" class="nom" />
<?php
class Formxxx {
function name_fieldxxx($name, $options, $value) {
switch($type)
{
case 'text' :
$this->addElement($name, array('type'=>'text','options'=>array('Size'=>'','Maxlength'=>'')), $value);
break;
}
}
}
$frmxxx = new Formxxx;
$frmxxx->name_fieldxxx('le nom', 'les options', 'la valeur');
?>
<?php
class Form {
public $type;
public $name;
public $size;
public $maxlength;
public $id;
public $class;
public function __construct($type, $name, $size, $maxlength, $id, $class) {
$this->type = $type;
$this->name = $name;
$this->size = $size;
$this->maxlength = $maxlength;
$this->id = $id;
$this->class = $class;
}
public function input_field()
{
echo '<input type="'.$type.'" name="'.$name.'" size="'.$size.'" maxlength="'.$maxlength.'" id="'.$id.'" class="'.$class.'" />';
}
}
$frm = new Form;
$frm->input_field('text', 'lenom', '30', '50', 'ids', 'classes');
?>
merci
<?php
class Form {
// propriétés de Form
public $name;
public $method;
public $action;
// construction de l'objet Form
public function __construct($name="", $method="get", $action="") { // aucun champ n'est obligatoire
$this->name = $name;
$this->method = $method;
$this->action = $action;
// imprimer l'entête
echo "<form name=\"$name\" method=\"$method\" action=\"$action\" >";
}
// terminer le Form
public function terminer()
{
echo "</form>";
}
}
class InputField {
// propriétés de InputField
public $name;
public $value;
public $type;
public $label;
public $id;
public $class;
public $size;
public $maxlength;
// construction de l'objet InputField
public function __construct($name, $value="", $type="text", $label="", $id="", $class="", $size="auto", $maxlength="auto") // seul le champ name est obligatoire
{
$this->name = $name;
$this->value = $value;
$label = trim($label)==""?strtoupper($name):$label; // si label est vide le remplacer par name en majuscules
$this->label = $label;
$id = trim($id)==""?$name:$id; // si id est vide le remplacer par name
$this->id = $id;
$this->class= $class;
$this->type = $type;
$this->size= $size;
$this->maxlength = $maxlength;
// imprimer le code HTML de l'InputField
echo "<label for=\"$name\">$label :</label><input type=\"$type\" name=\"$name\" value=\"$value\" size=\"$size\" maxlength=\"$maxlength\" id=\"$id\" class=\"$class\" />";
}
}
// test des classes
// voici le cas d'utilisation de l'objet Form
$f1 = new Form("monForm1"); // imprimer l'entête du Form
// voici les cas d'utilisation de l'objet InputField
new InputField("nom", "", "text"); // spécifie : name, value et type de l'objet InputField
new InputField("prenom"); // ne spécifie que name les autres propriétés de l'objet InputField ont des valeurs par défaut
new InputField("action", "enregistrer", "submit"); // spécifie : name, value et type de l'objet InputField
$f1->terminer(); // imprimer la fin du Form
// voici le cas d'utilisation de l'objet Form avec rappel des données postées par la méthode POST
$f2 = new Form("monForm2", "post");
//
new InputField("pseudo", $_POST['pseudo']); // champ de type text avec rappel de la valeur postée en cas de réaffichage
new InputField("mot de passe", "", "password"); // champ de type password
new InputField("action", "enregistrer", "submit");
//
$f2->terminer();
?>
########### Class des Inputs
class RadioField {
// propriétés de InputField
public $name;
public $value;
public $type;
public $id;
public $class;
// construction de l'objet RadioField
public function __construct($name, $value="", $type="radio", $id="", $class="")
{
$this->name = $name;
$this->value = $value;
$id = trim($id)==""?$name:$id;
$this->id = $id;
$this->class= $class;
$this->type = $type;
echo "<input type=\"$type\" name=\"$name\" value=\"$value\" id=\"$id\" class=\"$class\" />";
}
}
$f3 = new Form("monForm3", "post");
//
new RadioField("radio1", "Small", 'radio');
new RadioField("genre");
new RadioField("action", "envoyer", "submit");
//
$f3->terminer();
dans le cas des: radio, checkbox, text et password, ils ont tous <input>, je peux faire comme ca:public function __construct($name, $value="", $type="", $id="", $class="", $size="auto", $maxlength="auto")
ca marche pour text et password, alors que pour les radio et checkbox, il n'existe pas les attributs size et maxlength, est ce qu'il existe une methode pour combiner les 4 (text, password, radio, checkbox) en une seule methode?