Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Proste Tworzenie formularza
zietas
post
Post #1





Grupa: Zarejestrowani
Postów: 18
Pomógł: 0
Dołączył: 11.06.2007

Ostrzeżenie: (0%)
-----


Raczkuje w OOP.
Napisałem nasępujący powiedzmy sobie szczerze 'bardzo prosty generator formularza'.
  1. <?php
  2. class FormGenerator
  3. {
  4.  
  5. public function formOpen($action,$file)
  6. {
  7. if($file)
  8. {
  9. return $formStart = '<form method="post" action="'.$action.'" enctype="multipart/form-data">';
  10. }
  11. else
  12. {
  13. return $formStart = '<form method="post" action="'.$action.'">';
  14. }
  15. }
  16.  
  17. public function formInput($label,$type,$name)
  18. {
  19. return $input = ' <label for="'.$name.'">'.$label.'</label>: <input type="'.$type.'" name="'.$name.'" id="'.$name.'" />';
  20. }
  21.  
  22. public function formTextarea($label,$name,$char_width,$num_lines)
  23. {
  24. return $textarea = '<label for="'.$name.'">'.$label.'</label>: <textarea name="'.$name.'" cols="'.$char_width.'" rows="'.$num_lines.'" id="'.$name.'"></textarea>';
  25. }
  26.  
  27. public function formSelect($label,$name,$value,$option)
  28. {
  29. return $select = '<label for="'.$name.'">'.$label.'</label>: <select name="'.$name.'" id="'.$name.'"><option value="'.$value.'">'.$option.'</option></select>';
  30. }
  31.  
  32. public function formBtn($type,$value,$close)
  33. {
  34. if($close)
  35. {
  36. return $input = '<input type="'.$type.'" value="'.$value.'" /></form>';
  37. }
  38. else
  39. {
  40. return $input = '<input type="'.$type.'" value="'.$value.'" />';
  41. }
  42. }
  43. }
  44. ?>


No i moj formularz tworze nastepujaco:
  1. <?php
  2. require_once('class.FormGenerator.php');
  3.  
  4. $form = new FormGenerator();
  5.  
  6. echo $form->formOpen('contact.php',false);
  7. echo $form->formInput('Pierwszy','text','pierwszy_text');
  8. echo $form->formInput('Drugi','text','drugi_text');
  9. echo $form->formBtn('submit','Send',true);
  10. ?>


Czy jest to w miare poprawne?
Czy musze inaczej do tego podejsc? Jezeli tak, to jak?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
matix
post
Post #2





Grupa: Zarejestrowani
Postów: 278
Pomógł: 10
Dołączył: 13.02.2007
Skąd: Rybnik

Ostrzeżenie: (0%)
-----


Z nudów, napisałem sobie klasę, opierając się na tym, co napisał @Seth:

  1. <?
  2. interface Inputs {
  3. public function __toString();
  4. }
  5.  
  6. class Input implements Inputs {
  7. public function __construct($type, $name, $value = '')
  8. {
  9. $this->sInput = sprintf('<input type="%s" name="%s" value="%s"/>', $type, $name, $value);
  10. }
  11.  
  12. public function __toString()
  13. {
  14. return $this->sInput;
  15. }
  16. }
  17.  
  18. class form {
  19.  
  20. private $sResult = '';
  21.  
  22. function add(Inputs $input = null)
  23. {
  24. $this->sResult .= $input.$this->sLimit;
  25. return $this;
  26. }
  27.  
  28. function __toString()
  29. {
  30. return $this->sResult;
  31. }
  32.  
  33. function setLimit($string)
  34. {
  35. $this->sLimit = $string;
  36. }
  37.  
  38. }
  39.  
  40. $f = new form;
  41.  
  42. $f->setLimit("<br/>\n");
  43.  
  44. $f
  45. ->add (new Input('text', 'login'))
  46. ->add (new Input('text', 'password'))
  47. ->add (new Input('text', 'mail'))
  48. ->add (new Input('submit', 'send', 'sendit'));
  49.  
  50. echo $f;
  51.  
  52. ?>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 14.10.2025 - 07:51