Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Form Helper
<?php
/**
* FormHelper
*
* @package veedeoo HMVC
* @author @Veedeoo and @Geegler
* @copyright 2014
* @version 2.1.0
* @access public
*/
class FormHelper
{
/**
* FormHelper::__construct()
*
* @return
*/
public function __construct()
{
}
/**
* FormHelper::form_option()
*
* @param mixed $method
* @param mixed $action
* @param mixed $id
* @param mixed $class
* @return
*/
public function form_option($method, $action, $id = null, $class = null)
{
return '<form method="' . $method . '" action="' . $action . '" id="' . $id .
'" class="' . $class . '">';
}
/**
* FormHelper::form_label()
*
* @param mixed $name
* @param mixed $class
* @param mixed $id
* @return
*/
public function form_label($name, $class = null, $id = null)
{
return '<label class="' . $class . '" id="' . $id . '">' . $name . '</label>';
}
/**
* FormHelper::form_input()
*
* @param mixed $type
* @param mixed $name
* @param mixed $value
* @param mixed $class
* @param mixed $placeholder
* @return
*/
public function form_input($type, $name, $value = null, $class = null, $placeholder = null)
{
if (!empty($type)) {
return '<input type="' . $type . '" name="' . $name . '" value="' . $value .
'" class="' . $class . '" placeholder="' . $placeholder . '"/>';
}
return;
}
/**
* FormHelper::form_submit()
*
* @param mixed $name
* @param mixed $value
* @param mixed $class
* @param mixed $id
* @return
*/
public function form_submit($name, $value, $class = null, $id = null)
{
return '<input type="submit" name="' . $name . '" value="' . $value .
'" class="' . $class . '" id="' . $id . '"/>';
}
/**
* FormHelper::form_select()
*
* @param mixed $items
* @param mixed $name
* @return
*/
public function form_select($items = array(), $name)
{
$select = '';
if (is_array($items)) {
$select .= '<select name="' . $name . '">';
foreach ($items as $item) {
$select .= '<option value="' . $item . '">' . ucfirst($item) . '</option>';
}
$select .= '</select>';
}
return $select;
}
/**
* FormHelper::form_textarea()
*
* @param mixed $name
* @param int $rows
* @param int $cols
* @param mixed $extra
* @return
*/
public function form_textarea($name, $rows = null, $cols = null, $extra = null)
{
return ('<textarea name="' . $name . '" rows="' . $rows . '" cols="' . $cols .
'" ' . $extra . ' ></textarea>');
}
/**
* FormHelper::form_close()
*
* @return
*/
public function form_close()
{
return '</form>';
}
}
/*
* to use this class, please see example below
*/
$form_create = new FormHelper();
$select_array = array('php','java','html','javascript','python','perl','jquery');
echo $form_create->form_option('post', 'processor.php', 'myform', 'coolform');
echo $form_create->form_label('Name', 'name');
echo $form_create->form_input('text', 'name', '', 'input_class', 'type your name here');
echo '<br/>';
echo $form_create->form_label('Select Language','lang');
echo $form_create->form_select($select_array,'language');
echo '<br/>';
echo $form_create->form_submit('submit', 'Submit', 'submit_button', 'submit');
echo $form_create->form_close();
/*
* the above should output something like this
*/
/*
<form method="post" action="processor.php" id="myform" class="coolform">
<label class="name" id="">Name</label>
<input type="text" name="name" value="" class="input_class" placeholder="type your name here"/>
<br/>
<label class="lang" id="">Select Language</label>
<select name="language">
<option value="php">Php</option>
<option value="java">Java</option>
<option value="html">Html</option>
<option value="javascript">Javascript</option>
<option value="python">Python</option>
<option value="perl">Perl</option>
<option value="jquery">Jquery</option>
</select>
<br/>
<input type="submit" name="submit" value="Submit" class="submit_button" id="submit"/>
</form>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.