Aller au contenu

syntaxe class formulaire en php


Luckyluk

Sujets conseillés

salut à tous,

je sèche pour utiliser une class prise sur : http://www.supinfo-projects.com/fr/2006/cr..._form_php_fr/1/

autant je crois comprendre comment fonctionne cette class, autant la syntaxe ou methode d'utilisation m'echape :nonono:

j'ai donc rassembler les deux : class et utilisation dans le meme fichier que j'ai appeler formulaire.php


<?php
class Formulaire
{
var $name;
var $methode;
var $action;
var $champ_fichier = false;
var $tab_champs = array();
var $champ_default_size = 15;


function Formulaire($parameters)
{
$this->name = parameters("name");
$this->method = parameters("method");
$this->action = parameters("action");
}

function addInputText($parameters)
{
$parameters = array_merge(array("TYPE"=>"TEXT"), $parameters);
$this->AddChampToTab($parameters);
}

function addInputPassword($parameters)
{
$parameters = array_merge(array("TYPE"=>"PASSWORD"), $parameters);
$this->AddChampToTab($parameters);
}

function addInputHidden($parameters)
{
$parameters = array_merge(array("TYPE"=>"HIDDEN"), $parameters);
$this->AddChampToTab($parameters);
}

function addInputSubmit($parameters)
{
$parameters = array_merge(array("TYPE"=>"SUBMIT"), $parameters);
$this->AddChampToTab($parameters);
}

function debutForm()
{
$form = "<form method=\"".$this->method. "\" ";
$form .= "action=\"".this->action."\" ";
$form .= "name=\"".this->name."\" ";

if ($this->champ_fichier)
$form.="enctype=\"multipart/form-data\" ";

$form.= ">";

return $form;
}
function finForm()
{
$form ="</form>";
return $form;
}

function getChamp($nomChamp)
{
for ($counter=0; $counter<count($this->tab_champs); $counter++)
{
if ($this-tab_champs[$counter]["NAME"] == $nomChamp)
{
return $this->createInputs($this->tab_champs[counter])
}
}
return "";
}

function getLabel($nomChamp)
{
foreach ($this->tab_champs as $champ)
{
if ($champ["NAME"] == $nomChamp)
return $this->createLabel($champ);
}
return "";
}

function AddChampToTab($parameters)
{
$tab_size= count($this->tab_champs);
$this->tab_champs[tab_size] = parameters;
}

function CreateInputs($parameters)
{
switch(strtoupper($parameters["TYPE"]))
{
case "TEXT":
case "PASSWORD":
case "SUBMIT":
case "RESET":
case "FILE":
case "HIDDEN":
case "IMAGE":
if ($parameters["TYPE"] == "FILE")
$this->champ_fichier = true;

$champ = $this->genInput(parameters);
break;
case "SELECT":
$champ = $this->genSelect($parameters);
break;
case "CHECKBOX":
$champ = $this->genCheckbox($parameters);
break;
case "RADIO"
$champ = $this->genRadio($parameters);
break;
case "TEXTAREA"
$champ = $this->genTextarea($parameters);
break;
case "LABEL"
$champ = $this->genLabel($parameters);
break;

default:
$champ = "Type de champ inconnu";
break;
}
return $champ;
}
function genInputs($parameters)
{
$input = "<input type=\"".strtolower($parameters["TYPE"])."\" ";
$input.="name=\"".$parameters["NAME"]."\" ";

if(isset($parameters["VALUE"]))
$input.= "value\".$parameters["VALUE"]."\" ";

if($parameters["TYPE"]=="IMAGE" && isset($parameters["SRC"]))
$input.="src="".$parameters["SRC"]."\" ";

if($parameters["TYPE"]=="IMAGE" && isset($parameters["ALT"]))
$input.="src="".$parameters["ALT"]."\" ";
if(isset($parameters["SIZE"]))
$input .= "size=\"".$parameters["SIZE"]."\" ";
else
$input .= "size=\"".$this->champ_default_size."\" ";
if(isset($parameters["MAXLENGTH"]))
$input.="maxlength="".$parameters["MAXLENGTH"]."\" ";
if(isset($parameters["READONLY"]))
$input.= "readonly="readonly\" ";

$input .=">";

return $input;
}
function GenSelect($parameters)
{
$select = "<select name=\"".$parameters["NAME"]."\" ";

if(isset($parameters["SIZE"]))
$select.= "size=\"".$parameters["SIZE"]."\" ";
else
$select.= "size=\"1\" ";

$select .=">";
foreach ($parameters["OPTIONS"] as $option)
{
if(!$option["VALUE"])
$option["VALUE"] = $option["INTITULE"];
if(!$option["INTITULE"])
$option["INTITULE"] = $option["VALUE"];

$select .="<option value=\"".$option["VALUE"]."\">.
$option["INTITULE"]."</option>;
if(isset($parameters["SELECTED"]))
{
if ($parameters["SELECTED"] == $option["VALUE"]°
{
$select .="<option value=\"".$option["VALUE"].
"\" selected=\"selected\">".
$option["INTITULE"]."</option>";
$intituleChampSelected = $option["INTITULE"];
}
}
}
$select .= "</select>";
return $select;
}
function genCheckbox($parameters)
{
foreach $parameters["OPTION"] as $option)
{
$parameters["LABEL"] = $option["INTITULE"];
$checkbox .= "<input type=\"checkbox\" name=\"".
$parameters["NAME"]."[]"."\" ";
$checkbox .= "value=\"".$option["VALUE"]."\" ";

if ($parameters["CHECKED"])
if (in_array($option["VALUE"], $parameters["CHECKED"]))
$checkbox .= "checked=\"checked\" ";
$checkbox .= "> "
$parametersLabel = array(
"NAME"=>$parameters["NAME"].$option["VALUE"],
"LABEL"=>$parameters["LABEL"]
);
$checkbox .= $this->createLabel^$parametersLabel);
}
return $checkbox;
}
function genRadio($parameters)
{
foreach ($parameters["OPTION] as $option)
{
$parameters["LABEL"] = $option["INTITULE"];
$radio .= "<input type=\"radio\" name=\"".
$parameters["NAME"]."\" ";

if (!$option["VALUE"])
$option["VALUE"] = $option["INTITULE"];

$radio.= "value=\"".$option["VALUE"]."\" ";
if($parameters["CHECKED"] == $option["VALUE"])

$radio .= "checked=\"checked\" ";
if isset($parameters["READONLY"]) || $this->FORM_READONLY)
$radio .= "disabled=\"disabled\" ";
$radio .= "> ";
$parametersRadio = array(
"NAME"=>$parameters["NAME"].$option["VALUE"],
"LABEL"=>$parameters["LABEL"]
);
$radio .= $this->createLabel($parametersRadio);
}
return $radio;
}
function genTextarea($parameters)
{
$textarea = "<textarea name=\"".$parameters["NAME"]."\" ";
$textarea.= 'cols="'.$parameters["COLS"].'" ';
$textarea.= 'rows="'.$parameters["ROWS"].'" ';

if (isset($parameters["READONLY"]) || $this->FORM_READONLY)
$textarea .= "readonly=\"readonly\" ";

$textarea .=">";

if (isset(parameters["VALUE"]))
$textarea .= $parameters["VALUE"];

$textarea .= "</textarea>";

return $textarea;
}
function genLabel($parameters)
{
$label = "<label for=\"".$parameters["NAME"]."\">".
$parameters["LABEL"]."</label>";
return $label;
}
}
?>
<?php
//utilisation de la classe formulaire
$mon_formulaire =new Formulaire(array(
"NAME"=>"form_user",
"METHOD"=>"post",
"ACTION"=>""
));

$mon_formulaire->addInputText(array(
"LABEL"=>"nom: ",
"NAME"=>"user_first_name"
));
$mon_formulaire->addInputText(array(
"LABEL"=>"prénom: ",
"NAME"=>"user_last_name"
));

$mon_formulaire->addInputSubmit(array(
"NAME"=>"user_valider",
"VALUE"=>"valider"
));
?>
<html>
<body>
<table>
<?php echo$mon_formulaire->debutForm();?>
<tr>
<td><?php echo $mon_formulaire->getLabel("user_first_name");?></td>
<td><?php echo $mon_formulaire->getChamp("user_first_name");?></td>
</tr>
<tr>

<td><?php echo $mon_formulaire->getChamp("user_valider");?></td>
</tr>
<?php echo $mon-formulaire->finForm();?>
</table>
</body>
</html>

je l'ai mis en ligne ici pour les effets mais ça bug !

http://www.tonsite.net/vrac/formulaire/formulaire.php

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/users2/t/tonsite/www/vrac/formulaire/formulaire.php on line 46

autrement dit : ça bug là des que l'on essaye d'utiliser le $this-> pas l'air d'avoir pris en compte le constructeur

 
function debutForm()
{
$form = "<form method=\"".$this->method. "\" ";
$form .= "action=\"".this->action."\" ";
$form .= "name=\"".this->name."\" ";

if ($this->champ_fichier)
$form.="enctype=\"multipart/form-data\" ";

$form.= ">";

return $form;
}

merci d'avance pour le coup de pouce

Lien vers le commentaire
Partager sur d’autres sites

merci.. j'ai fait les modifs...

a chaque resolution, amène un nouveau probleme ( je prefererai l'inverse ;) )

autre questions, apparemment il n'aime pas les [ du style d'en dessou

Parse error: syntax error, unexpected '[' in /home/users2/t/tonsite/www/vrac/formulaire/formulaire.php on line 66

if ($this-tab_champs($counter)["NAME"] == $nomChamp)

j'en avait deja enlever et remplacer par des ( mais je crains que celà change le contenu...

(d'ailleurs dans le cas ci dessu, ça ne resoud pas le probleme..

il y une regle particulière à l'utilisation de crochet?

une solutions?

encore merci

Lien vers le commentaire
Partager sur d’autres sites

j'ai modifié...

mais non apparemment c'est pas ça...

j'avais deja du enlever ça de là: (dans le constructeur)...


<?php
class Formulaire
{
var $name;
var $method;
var $action;
var $champ_fichier = false;
var $tab_champs = array();
var $champ_default_size = 15;


function Formulaire($parameters)
{
$this->name = parameters("name");
$this->method = parameters("method");
$this->action = parameters("action");
}

function addInputText($parameters)
{
$parameters = array_merge(array("TYPE"=>"TEXT"), $parameters);
$this->AddChampToTab($parameters);
}

function addInputPassword($parameters)
{
$parameters = array_merge(array("TYPE"=>"PASSWORD"), $parameters);
$this->AddChampToTab($parameters);
}

function addInputHidden($parameters)
{
$parameters = array_merge(array("TYPE"=>"HIDDEN"), $parameters);
$this->AddChampToTab($parameters);
}

function addInputSubmit($parameters)
{
$parameters = array_merge(array("TYPE"=>"SUBMIT"), $parameters);
$this->AddChampToTab($parameters);
}

function debutForm()
{
$form = "<form method=\"".$this->method. "\" ";
$form .= "action=\"".$this->action."\" ";
$form .= "name=\"".$this->name."\" ";

if ($this->champ_fichier)
$form.="enctype=\"multipart/form-data\" ";

$form.= ">";

return $form;
}
function finForm()
{
$form ="</form>";
return $form;
}

function getChamp($nomChamp)
{
for ($counter=0; $counter<count($this->tab_champs); $counter++)
{
if ($this->tab_champs($counter)["NAME"] == $nomChamp)
{
return $this->createInputs($this->tab_champs(counter))
}
}
return "";
}

function getLabel($nomChamp)
{
foreach ($this->tab_champs as $champ)
{
if ($champ["NAME"] == $nomChamp)
return $this->createLabel($champ);
}
return "";
}

function AddChampToTab($parameters)
{
$tab_size= count($this->tab_champs);
$this->tab_champs[tab_size] = parameters;
}

function CreateInputs($parameters)
{
switch(strtoupper($parameters["TYPE"]))
{
case "TEXT":
case "PASSWORD":
case "SUBMIT":
case "RESET":
case "FILE":
case "HIDDEN":
case "IMAGE":
if ($parameters["TYPE"] == "FILE")
$this->champ_fichier = true;

$champ = $this->genInput(parameters);
break;
case "SELECT":
$champ = $this->genSelect($parameters);
break;
case "CHECKBOX":
$champ = $this->genCheckbox($parameters);
break;
case "RADIO"
$champ = $this->genRadio($parameters);
break;
case "TEXTAREA"
$champ = $this->genTextarea($parameters);
break;
case "LABEL"
$champ = $this->genLabel($parameters);
break;

default:
$champ = "Type de champ inconnu";
break;
}
return $champ;
}
function genInputs($parameters)
{
$input = "<input type=\"".strtolower($parameters["TYPE"])."\" ";
$input.="name=\"".$parameters["NAME"]."\" ";

if(isset($parameters["VALUE"]))
$input.= "value\".$parameters["VALUE"]."\" ";

if($parameters["TYPE"]=="IMAGE" && isset($parameters["SRC"]))
$input.="src="".$parameters["SRC"]."\" ";

if($parameters["TYPE"]=="IMAGE" && isset($parameters["ALT"]))
$input.="src="".$parameters["ALT"]."\" ";
if(isset($parameters["SIZE"]))
$input .= "size=\"".$parameters["SIZE"]."\" ";
else
$input .= "size=\"".$this->champ_default_size."\" ";
if(isset($parameters["MAXLENGTH"]))
$input.="maxlength="".$parameters["MAXLENGTH"]."\" ";
if(isset($parameters["READONLY"]))
$input.= "readonly="readonly\" ";

$input .=">";

return $input;
}
function GenSelect($parameters)
{
$select = "<select name=\"".$parameters["NAME"]."\" ";

if(isset($parameters["SIZE"]))
$select.= "size=\"".$parameters["SIZE"]."\" ";
else
$select.= "size=\"1\" ";

$select .=">";
foreach ($parameters["OPTIONS"] as $option)
{
if(!$option["VALUE"])
$option["VALUE"] = $option["INTITULE"];
if(!$option["INTITULE"])
$option["INTITULE"] = $option["VALUE"];

$select .="<option value=\"".$option["VALUE"]."\">.
$option["INTITULE"]."</option>;
if(isset($parameters["SELECTED"]))
{
if ($parameters["SELECTED"] == $option["VALUE"]°
{
$select .="<option value=\"".$option["VALUE"].
"\" selected=\"selected\">".
$option["INTITULE"]."</option>";
$intituleChampSelected = $option["INTITULE"];
}
}
}
$select .= "</select>";
return $select;
}
function genCheckbox($parameters)
{
foreach $parameters["OPTION"] as $option)
{
$parameters["LABEL"] = $option["INTITULE"];
$checkbox .= "<input type=\"checkbox\" name=\"".
$parameters["NAME"]."[]"."\" ";
$checkbox .= "value=\"".$option["VALUE"]."\" ";

if ($parameters["CHECKED"])
if (in_array($option["VALUE"], $parameters["CHECKED"]))
$checkbox .= "checked=\"checked\" ";
$checkbox .= "> "
$parametersLabel = array(
"NAME"=>$parameters["NAME"].$option["VALUE"],
"LABEL"=>$parameters["LABEL"]
);
$checkbox .= $this->createLabel^$parametersLabel);
}
return $checkbox;
}
function genRadio($parameters)
{
foreach ($parameters["OPTION] as $option)
{
$parameters["LABEL"] = $option["INTITULE"];
$radio .= "<input type=\"radio\" name=\"".
$parameters["NAME"]."\" ";

if (!$option["VALUE"])
$option["VALUE"] = $option["INTITULE"];

$radio.= "value=\"".$option["VALUE"]."\" ";
if($parameters["CHECKED"] == $option["VALUE"])

$radio .= "checked=\"checked\" ";
if isset($parameters["READONLY"]) || $this->FORM_READONLY)
$radio .= "disabled=\"disabled\" ";
$radio .= "> ";
$parametersRadio = array(
"NAME"=>$parameters["NAME"].$option["VALUE"],
"LABEL"=>$parameters["LABEL"]
);
$radio .= $this->createLabel($parametersRadio);
}
return $radio;
}
function genTextarea($parameters)
{
$textarea = "<textarea name=\"".$parameters["NAME"]."\" ";
$textarea.= 'cols="'.$parameters["COLS"].'" ';
$textarea.= 'rows="'.$parameters["ROWS"].'" ';

if (isset($parameters["READONLY"]) || $this->FORM_READONLY)
$textarea .= "readonly=\"readonly\" ";

$textarea .=">";

if (isset(parameters["VALUE"]))
$textarea .= $parameters["VALUE"];

$textarea .= "</textarea>";

return $textarea;
}
function genLabel($parameters)
{
$label = "<label for=\"".$parameters["NAME"]."\">".
$parameters["LABEL"]."</label>";
return $label;
}
}
?>
<?php
//utilisation de la classe formulaire
$mon_formulaire =new Formulaire(array(
"NAME"=>"form_user",
"METHOD"=>"post",
"ACTION"=>""
));

$mon_formulaire->addInputText(array(
"LABEL"=>"nom: ",
"NAME"=>"user_first_name"
));
$mon_formulaire->addInputText(array(
"LABEL"=>"prénom: ",
"NAME"=>"user_last_name"
));

$mon_formulaire->addInputSubmit(array(
"NAME"=>"user_valider",
"VALUE"=>"valider"
));
?>
<html>
<body>
<table>
<?php echo$mon_formulaire->debutForm();?>
<tr>
<td><?php echo $mon_formulaire->getLabel("user_first_name");?></td>
<td><?php echo $mon_formulaire->getChamp("user_first_name");?></td>
</tr>
<tr>

<td><?php echo $mon_formulaire->getChamp("user_valider");?></td>
</tr>
<?php echo $mon-formulaire->finForm();?>
</table>
</body>
</html>


Lien vers le commentaire
Partager sur d’autres sites

Veuillez vous connecter pour commenter

Vous pourrez laisser un commentaire après vous êtes connecté.



Connectez-vous maintenant
×
×
  • Créer...