Posted By

JonnySnip3r on 06/11/11


Tagged

form class php html make easy h0x


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tux-world


php Form Class


 / Published in: PHP
 

Just a random php form class I made a while back ;-) Enjoy!

  1. <?php
  2.  
  3. class JCForm
  4. {
  5. // @var $_legend_text -> Set the legend text.
  6. private $_legend_text = 'Change Me!';
  7. // @var $_enctype -> What's the enctype?
  8. private $_enctype = 'multipart/form-data';
  9.  
  10. /**
  11.   * @author Jonno <[email protected]>
  12.   *
  13.   * @method This method creates the foundation of a form, it will create the opening fieldset and form
  14.   * tags. Remember to use the end_form function when closing the form.
  15.   *
  16.   * @param string $method -> Supply the form with the method. (POST or GET)
  17.   * @param string $action -> (Optional) Suppy the form with an action, so where will it redirect when submitted?
  18.   * @param string $class -> (Optional) Supply the form with a class, this is useful for styling with CSS
  19.   * @param string $id -> (Optional) Supply the form with an ID, useful for styling or for AJAX
  20.   * @param bool $enctype -> (Optional) Does this form require an enctype? If so please specify which type before creating the form (Default: 'multipart/form-data')
  21.   *
  22.   * @return string $form -> Returns the actual opening tags of a form, along with any additional information supplied.
  23.   */
  24. public function start_form($method, $action = '#', $legend = false, $class = NULL, $id = NULL, $enctype = false)
  25. {
  26. //
  27.  
  28. if(!empty($class)) $class = "class='$class'"; else $class = NULL;
  29. if(!empty($id)) $id = "id='$id'"; else $id = NULL;
  30. if($enctype == true) $enctype = "enctype='$this->_enctype'"; else $enctype = NULL;
  31.  
  32. // The form will be correcly indented.
  33. $form = "\t<fieldset>\n";
  34. if($legend == true) $form .= "\t\t<legend>$this->_legend_text</legend>\n";
  35. $form .= "\t\t<form method='$method' action='$action' $class $id $enctype>\n";
  36.  
  37. return $form;
  38. }
  39.  
  40. /**
  41.   * @author Jonno <[email protected]>
  42.   *
  43.   * @method This method creates an input such as a textfield, radio input etc. Should be called after the start_form
  44.   * method.
  45.   *
  46.   * @param string $type -> This is the type of input you are after. For example text, password, radio etc.
  47.   * @param string $name -> Set the name of the input. For example Username, Password, Email.
  48.   * @param string $value -> Set the value of an input.
  49.   * @param string $class -> (Optional) Apply a class to the input. Used for CSS etc.
  50.   * @param string $id -> (Optional) Give the form an ID useful for javascript etc.
  51.   * @param bool $disabled -> (Optional) Should this be disabled? (True or False)
  52.   * @return string $input -> Returns the input all pretty and finished.
  53.   */
  54. public function create_input($type = 'text', $name = '', $value = '', $class = NULL, $id = NULL, $disabled = false, $placeholder = '')
  55. {
  56. if(!empty($class)) $class = "class='$class'"; else $class = NULL;
  57. if(!empty($id)) $id = "id='$id'"; else $id = NULL;
  58. if($disabled == true) $disabled = "disabled='disabled'"; else $disabled = NULL;
  59. if(!empty($placeholder)) $placeholder = "placeholder='$placeholder'"; else $placeholder = NULL;
  60.  
  61. $input = "\t\t\t<input type='$type' name='$name' value='$value' $class $id $disabled $placeholder/>\n";
  62. return $input;
  63. }
  64.  
  65. /**
  66.   * @author Jonno <[email protected]>
  67.   *
  68.   * @method This method creates a Textarea. Very simple only requires one argument.
  69.   *
  70.   * @param string $name -> Name the comment form.
  71.   * @param int $rows -> (Optional) How many rows should this have? Default 2
  72.   * @param int $cols -> (Optional) How many columns should this have? Default 20
  73.   * @param string $value -> (Optional) Should this have a default value?
  74.   * @param string $class -> (Optional) Does this have a class? Good for CSS
  75.   * @param string $id -> (Optional) Does this have an id?
  76.   * @param bool $disabled -> (Optional) Should this be disabled? (True or False)
  77.   * @return string -> Returns the textarea, nice and complete.
  78.   */
  79. public function create_textarea($name = '', $rows = 2, $cols = 20, $value = NULL, $class = NULL, $id = NULL, $disabled = false, $placeholder = '')
  80. {
  81. if(!empty($class)) $class = "class='$class'"; else $class = NULL;
  82. if(!empty($id)) $id = "id='$id'"; else $id = NULL;
  83. if($disabled == true) $disabled = "disabled='disabled'"; else $disabled = NULL;
  84. if(!empty($placeholder)) $placeholder = "placeholder='$placeholder'"; else $placeholder = NULL;
  85.  
  86. $textarea = "\t\t\t<textarea rows='$rows' cols='$cols' $class $id $disabled $placeholder>$value</textarea>\n";
  87. return $textarea;
  88. }
  89.  
  90. /**
  91.   * @author Jonno <[email protected]>
  92.   *
  93.   * @method This method changes the title of the legend that has been optionally added to the form,
  94.   * this also must be called before the creation of the form.
  95.   *
  96.   * @param string $title -> Sets the name of the legend.
  97.   */
  98. public function set_legend_title($title = NULL)
  99. {
  100. $this->_legend_text = $title;
  101. }
  102.  
  103. /**
  104.   * @author Jonno <[email protected]>
  105.   *
  106.   * @method Use this method to set the enctype of the form, by default its 'multipart/form-data'
  107.   *
  108.   * @param string $enctype -> Whats the enctype?
  109.   */
  110. public function set_enctype($enctype)
  111. {
  112. $this->_enctype = $enctype;
  113. }
  114.  
  115. /**
  116.   * @author Jonno <[email protected]>
  117.   *
  118.   * @method This method created the select tag along with any additional option tags. Options should be passed in as an array, see
  119.   * below for correct array format.
  120.   *
  121.   * @param string $name -> Name of the select.
  122.   * @param array $option -> Supply all of the options you would like inside this select. Supply in an array like so:
  123.   * option_value => 'Option Value', option_title => 'Option Title'
  124.   */
  125. public function create_select($name, $option, $class = NULL, $id = NULL)
  126. {
  127. $form = "\t\t\t<select name='$name'>\n";
  128. foreach($option as $option_value => $option_name)
  129. {
  130. $form .= "\t\t\t\t<option value='$option_value'>$option_name</option>\n";
  131. }
  132. $form .= "\t\t\t</select>\n";
  133. return $form;
  134. }
  135.  
  136. /**
  137.   * @author Jonno <[email protected]>
  138.   *
  139.   * @method This method will create the rest of the html form, it will do so by supplying the end of the form.
  140.   * This is used after the creat_form method is used.
  141.   *
  142.   * @return string $form -> Returns the closing tags of the form and the fieldset.
  143.   */
  144. public function close_form()
  145. {
  146. $form = "\t\t</form>\n";
  147. $form .= "\t</fieldset>\n";
  148. return $form;
  149. }
  150. }
  151.  
  152. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: rigobcastro on August 29, 2011

Great class, thanks for sharing...

You need to login to post a comment.