/ Published in: PHP
Just a random php form class I made a while back ;-) Enjoy!
Expand |
Embed | Plain Text
<?php class JCForm { // @var $_legend_text -> Set the legend text. private $_legend_text = 'Change Me!'; // @var $_enctype -> What's the enctype? private $_enctype = 'multipart/form-data'; /** * @author Jonno <coffeemakescode@gmail.com> * * @method This method creates the foundation of a form, it will create the opening fieldset and form * tags. Remember to use the end_form function when closing the form. * * @param string $method -> Supply the form with the method. (POST or GET) * @param string $action -> (Optional) Suppy the form with an action, so where will it redirect when submitted? * @param string $class -> (Optional) Supply the form with a class, this is useful for styling with CSS * @param string $id -> (Optional) Supply the form with an ID, useful for styling or for AJAX * @param bool $enctype -> (Optional) Does this form require an enctype? If so please specify which type before creating the form (Default: 'multipart/form-data') * * @return string $form -> Returns the actual opening tags of a form, along with any additional information supplied. */ public function start_form($method, $action = '#', $legend = false, $class = NULL, $id = NULL, $enctype = false) { // if($enctype == true) $enctype = "enctype='$this->_enctype'"; else $enctype = NULL; // The form will be correcly indented. $form = "\t<fieldset>\n"; if($legend == true) $form .= "\t\t<legend>$this->_legend_text</legend>\n"; $form .= "\t\t<form method='$method' action='$action' $class $id $enctype>\n"; return $form; } /** * @author Jonno <coffeemakescode@gmail.com> * * @method This method creates an input such as a textfield, radio input etc. Should be called after the start_form * method. * * @param string $type -> This is the type of input you are after. For example text, password, radio etc. * @param string $name -> Set the name of the input. For example Username, Password, Email. * @param string $value -> Set the value of an input. * @param string $class -> (Optional) Apply a class to the input. Used for CSS etc. * @param string $id -> (Optional) Give the form an ID useful for javascript etc. * @param bool $disabled -> (Optional) Should this be disabled? (True or False) * @return string $input -> Returns the input all pretty and finished. */ public function create_input($type = 'text', $name = '', $value = '', $class = NULL, $id = NULL, $disabled = false, $placeholder = '') { if($disabled == true) $disabled = "disabled='disabled'"; else $disabled = NULL; $input = "\t\t\t<input type='$type' name='$name' value='$value' $class $id $disabled $placeholder/>\n"; return $input; } /** * @author Jonno <coffeemakescode@gmail.com> * * @method This method creates a Textarea. Very simple only requires one argument. * * @param string $name -> Name the comment form. * @param int $rows -> (Optional) How many rows should this have? Default 2 * @param int $cols -> (Optional) How many columns should this have? Default 20 * @param string $value -> (Optional) Should this have a default value? * @param string $class -> (Optional) Does this have a class? Good for CSS * @param string $id -> (Optional) Does this have an id? * @param bool $disabled -> (Optional) Should this be disabled? (True or False) * @return string -> Returns the textarea, nice and complete. */ public function create_textarea($name = '', $rows = 2, $cols = 20, $value = NULL, $class = NULL, $id = NULL, $disabled = false, $placeholder = '') { if($disabled == true) $disabled = "disabled='disabled'"; else $disabled = NULL; $textarea = "\t\t\t<textarea rows='$rows' cols='$cols' $class $id $disabled $placeholder>$value</textarea>\n"; return $textarea; } /** * @author Jonno <coffeemakescode@gmail.com> * * @method This method changes the title of the legend that has been optionally added to the form, * this also must be called before the creation of the form. * * @param string $title -> Sets the name of the legend. */ public function set_legend_title($title = NULL) { $this->_legend_text = $title; } /** * @author Jonno <coffeemakescode@gmail.com> * * @method Use this method to set the enctype of the form, by default its 'multipart/form-data' * * @param string $enctype -> Whats the enctype? */ public function set_enctype($enctype) { $this->_enctype = $enctype; } /** * @author Jonno <coffeemakescode@gmail.com> * * @method This method created the select tag along with any additional option tags. Options should be passed in as an array, see * below for correct array format. * * @param string $name -> Name of the select. * @param array $option -> Supply all of the options you would like inside this select. Supply in an array like so: * option_value => 'Option Value', option_title => 'Option Title' */ public function create_select($name, $option, $class = NULL, $id = NULL) { $form = "\t\t\t<select name='$name'>\n"; foreach($option as $option_value => $option_name) { $form .= "\t\t\t\t<option value='$option_value'>$option_name</option>\n"; } $form .= "\t\t\t</select>\n"; return $form; } /** * @author Jonno <coffeemakescode@gmail.com> * * @method This method will create the rest of the html form, it will do so by supplying the end of the form. * This is used after the creat_form method is used. * * @return string $form -> Returns the closing tags of the form and the fieldset. */ public function close_form() { $form = "\t\t</form>\n"; $form .= "\t</fieldset>\n"; return $form; } } ?>
Comments
Subscribe to comments
You need to login to post a comment.

Great class, thanks for sharing...