Ver Mensaje Individual
  #1 (permalink)  
Antiguo 26/10/2011, 10:06
Avatar de tomark4
tomark4
 
Fecha de Ingreso: septiembre-2007
Mensajes: 154
Antigüedad: 16 años, 7 meses
Puntos: 29
De acuerdo Clase para crear Formularios en Php

Buenas aqui les dejo una contribucion es una clase que hice para generar formularios en php por ahora solo genera el formulario la validacion tendrias que agregarla desde javascript pero funciona bastante bien. pronto la mejorare.

class_form.php

Código PHP:
Ver original
  1. <?php
  2.  
  3. /******************************************************************************
  4. * html_form                                                                   *
  5. *                                                                             *
  6. * Version: 1.1                                                                *
  7. * Date:    2011-10-21                                                         *
  8. * Author:  Jose Quintero                                                      *
  9. *******************************************************************************/
  10. class html_form{
  11. var $name;
  12. var $action;
  13. var $method;
  14. var $enctype;
  15. var $onsubmit;
  16. var $form;
  17. var $input;
  18. var $type;
  19. var $label;
  20. var $value;
  21. var $oForm;
  22. var $cForm;
  23. var $radio;
  24. var $checkbox;
  25. var $select;
  26. var $texto;
  27. var $fieldset;
  28. var $legend;
  29. var $widht;
  30.  
  31.    
  32.     function openform($name='form1', $method='post', $action='#', $enctype='', $onsubmit=''){
  33.         $this->action = $action;
  34.         $this->method = $method;
  35.         $this->name = $name;
  36.         $this->enctype = $enctype;
  37.         $this->onsubmit = $onsubmit;
  38.        
  39.         $this->oform = "<form name='".$this->name."' action='".$this->action."' method='".$this->method."' enctype='".$this->enctype."' onsubmit='".$this->onsubmit."'>";
  40.         return $this->oform;
  41.     }
  42.    
  43.  
  44.     function addInput($type, $name, $label='',$value=''){
  45.         $this->type = $type;
  46.         $this->name = $name;
  47.         $this->label= $label;
  48.         $this->value = $value;
  49.  
  50.         if($this->type == "submit" || $this->type=="reset"){
  51.             $this->input = "<label></label>";
  52.         }
  53.         else{
  54.             $this->input= "<label>".$this->label."</label><br>";
  55.         }
  56.        
  57.         $this->input .= "<input type='".$this->type."' name='".$this->name."' value='".$this->value."' id='".$this->name."'/>";
  58.        
  59.         return $this->input;
  60.     }
  61.    
  62.    
  63.     function addcheckradio($type,$name,$values,$selected=0){
  64.    
  65.         unset($this->radio);
  66.        
  67.         $this->value = $values;
  68.         $this->selected = $selected;
  69.         $this->type = $type;
  70.        
  71.         if ($this->type=="checkbox"){
  72.             $this->name = $name."[]";
  73.         }
  74.         else{
  75.             $this->name = $name;
  76.         }
  77.        
  78.         $c=1;  
  79.         while(list($val,$l)=each($this->value)){   
  80.             if ($c==$this->selected){
  81.                 $this->check = " checked/>";
  82.             }
  83.             else{
  84.                 $this->check = " />";
  85.             }
  86.            
  87.             $this->radio .=  "<label>".$this->value[$val]."</label><input type='".$this->type."' name='".$this->name."' value='".$val."'".$this->check."<br>";
  88.             $c++;
  89.         }
  90.         return  $this->radio;
  91.     }
  92.    
  93.     function addTextarea($name, $cols=20, $rows=5, $label='',$value=''){
  94.         $this->name=$name;
  95.         $this->row= $rows;
  96.         $this->col= $cols;
  97.         $this->value = $value;
  98.         $this->label = $label;
  99.        
  100.         $this->textarea = "<label>".$this->label."</label><br><textarea name='".$this->name."' cols='".$this->col."' rows='".$this->row."'>".$this->value."</textarea>";
  101.         return $this->textarea;
  102.     }
  103.  
  104.     function addSelect($name, $values, $label='', $multiple=0){
  105.        
  106.         $this->values=$values;
  107.         $this->name=$name;
  108.         $this->label=$label;
  109.        
  110.         if($multiple==1){
  111.             $this->select = "<label>".$this->label."</label><br><select name='".$this->name."[]' multiple='multiple'>";
  112.         }
  113.         else{
  114.             $this->select = "<label>".$this->label."</label><br><select name='".$this->name."'>";
  115.         }
  116.        
  117.         while(list($values, $text)=each($this->values))
  118.         {      
  119.             $this->select .= "<option value='".$values."'>".$this->values[$values]."</option>";
  120.         }
  121.             $this->select  .= "</select>";
  122.  
  123.         return $this->select;
  124.     }
  125.    
  126.    
  127.    
  128.     function openfieldset($texto,$width='300'){
  129.         $this->legend=$texto;
  130.         $this->width=$width;
  131.        
  132.         $this->fieldset="<fieldset style='width:".$this->width."px;'><legend>".$this->legend."</legend>";
  133.         return $this->fieldset;
  134.     }
  135.    
  136.     function closefieldset(){
  137.        
  138.         $this->fieldset="</fieldset>";
  139.         return $this->fieldset;
  140.     }
  141.    
  142.     function closeform(){
  143.         $this->cform = "</form>";
  144.         return $this->cform;
  145.     }
  146. }
  147. ?>

aqui te muestro un ejemplo explicado de como se usa.

Código PHP:
Ver original
  1. <?php
  2. /*incluimos el archivo de la clase y creamos la instancia para generar
  3. el formulario*/
  4. include("class_form.php");
  5. $frm = new html_form();
  6.  
  7. /*
  8. Iniciar el formulario
  9. openform(nombre, metodo, accion, enctype, onsubmit)
  10. nombre = nombre del formulario
  11. metodo = get o post (por defecto es post)
  12. accion = el script que se ejecutara cuando se envie el formulario
  13. enctype = determina como sera codificada la data(application/x-www-form-urlencoded,
  14.                                                  multipart/form-data,
  15.                                                  text/plain)
  16. onsubmit = ejecutara el script de javascript para validar el formulario.
  17.  
  18. */
  19. $formulario = $frm->openform("form1","post","procesa_datos.php","multipart/form-data");
  20.  
  21. /*openfieldset(leyenda, tamaños)
  22. inicia el fieldset
  23. leyenda = texto que aparecera
  24. tamaño = ancho del fieldset
  25. */
  26. $formulario .= $frm->openfieldset("Cajas de Texto",300);
  27.  
  28. /*
  29. añadir input (texto, password, file, submit, reset)
  30. addInput(tipo, nombre, etiqueta, valor)
  31.     tipo = puede ser text, password, submit, reset
  32.     nombre =  nombre de la caja de texto sera usado tambien como id
  33.     etiqueta = etiqueta a mostrar en los (en los submit y reset no se usa)
  34.     valor = si mostrara algun valor por defecto(en el caso de submit y reset
  35.                                                 es el texto que mostrara el
  36.                                                 boton)
  37. */
  38. $formulario .= $frm->addInput("text","nombre","Nombres: ")."<br>";
  39. $formulario .= $frm->addInput("text","apellido","Apellidos: ")."<br>";
  40. $formulario .= $frm->addInput("password","password","Contraseña: ")."<br>";
  41. $formulario .= $frm->addInput("hidden","opcion","",1);
  42. $formulario .= $frm->addInput("file","foto","Foto: ");
  43. /*
  44. closefieldset
  45. cierra el fieldset
  46. */
  47. $formulario .= $frm->closefieldset();
  48.  
  49. /*
  50. añadir radio boton
  51. addcheckradio(tipo,nombre, valores, seleccionado)
  52.     tipo = puede ser checkbox o radio.
  53.     nombre = nombre del boton
  54.     valores =  array(valor1=>etiqueta1,valor2=>etiqueta2);
  55.         el array puede ser numerico o de texto.
  56.     seleccionado = cual sera seleccionado por defecto ninguno esta marcado
  57. */
  58. $formulario .= $frm->openfieldset("Radio Botones",300);
  59. $values = array("M"=>"Masculino","F"=>"Femenino");
  60. $formulario .=  $frm->addcheckradio("radio","sexo",$values,1);
  61. $formulario .= $frm->closefieldset();
  62.  
  63.  
  64. $formulario .= $frm->openfieldset("Radio Botones",300);
  65. $values = array(5=>"Excelente",4=>"Bueno",3=>"Regular",2=>"Malo",1=>"Deficiente");
  66. $formulario .= $frm->addcheckradio("radio","encuesta",$values,1);
  67. $formulario .= $frm->closefieldset();
  68.  
  69.  
  70. $values = array(1=>"Basket",2=>"Futbol",3=>"Tenis",4=>"Beisbol",5=>"Golf",
  71.                 6=>"Atletismo",7=>"Musica",8=>"Internet");
  72. $formulario .= $frm->openfieldset("Checkbox",300);
  73. $formulario .= $frm->addcheckradio("checkbox","hobbies",$values,1);
  74. $formulario .= $frm->closefieldset();
  75.  
  76. /*
  77. añadir area de textp
  78. addTextarea(nombre,columnas, filas, etiqueta,valor)
  79.     nombre = nombre de la caja de textp
  80.     columnas y filas = tamaño de la caja de texto(por defecto es 20 cols, 5 rows
  81.     Etiqueta =  valor que mostrara el label de la caja de texto
  82.     valor = si el texto mostrara algo por defecto
  83. */
  84. $formulario .= $frm->openfieldset("Area de Texto",300);
  85. $formulario .= $frm->addTextarea("direccion",20,5,"Direccion")."<br>";
  86. $formulario .= $frm->closefieldset();
  87.  
  88. /*
  89. añadir select option
  90. addSelect(nombre,valores, etiqueta,multiple)
  91.     nombre = nombre del select
  92.     valores = array tipo (valor1=>texto1,valor2=>texto2)
  93.     Etiqueta =  valor que mostrara el label del select
  94.     multiple = 1 para activar multiple, por defecto es desactivado.
  95. */
  96. $formulario .= $frm->openfieldset("Select Options",300);
  97. $valores=array("ven"=>"Venezuela","col"=>"Colombia");
  98. $formulario .= $frm->addSelect("paises",$valores, "Paises: ")."<br>";
  99.  
  100. $valores=array(1=>"Amarillo",2=>"Azul",3=>"Rojo");
  101. $formulario .= $frm->addSelect("colores",$valores, "Colores: ",1);
  102. $formulario .= $frm->closefieldset();
  103.  
  104. $formulario .= $frm->addInput("submit","enviar","","Enviar");
  105. $formulario .= $frm->addInput("reset","reset","","Reset");
  106.  
  107. /*
  108. cierra el formulario
  109. closeform()
  110. */
  111. $formulario .= $frm->closeform();
  112. echo $formulario;
  113.  
  114.  
  115. ?>

y este es un ejemplo de que recoge bien los datos enviados

Código PHP:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Documento sin título</title>
  6. </head>
  7.  
  8. <body>
  9. <?php
  10.  
  11. echo "Nombre: ".$_POST[nombre]."<br>";
  12. echo "Apellido: ".$_POST[apellido]."<br>";
  13. echo "Password: ".md5($_POST[password])."<br>";
  14. echo "Opcion: ".$_POST[opcion]."<br>";
  15. echo "Sexo: ".$_POST[sexo]."<br>";
  16. echo "Opinion del Sitio: ".$_POST[encuesta]."<br>";
  17.  
  18.  
  19. $hobbie = $_POST[hobbies];
  20. foreach($hobbie as $val){
  21.     echo "Hobbie: ".$val."<br>";
  22. }
  23. echo "Direccion: ".$_POST[direccion]."<br>";
  24. echo "Paises: ".$_POST[paises]."<br>";
  25.  
  26. $colores= $_POST[colores];
  27. foreach($colores as $val){
  28.     echo "Colores: ".$val."<br>";
  29. }
  30.  
  31. ?>
  32. </body>
  33. </html>

cualquier cosa me escriben por aqui o por el twitter @pctec21 gracias

Última edición por tomark4; 26/10/2011 a las 10:24