The purpose of this class is to be able to create an html form with ease. This is inspired by the form helper in CodeIgniter. This can easily adapt to bootstrap or any css and jquery where id is required.
To use this class, we need to include the class provided below.
require_once('FormHelper.php');
## we create an instance of the class
$form_create = new FormHelper();
we can also dynamically create <select>, by defining the option items as an array
$select_array = array('php','java','html','javascript','python','perl','jquery');
generate the form
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 give us this output
<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>
How to handle the parameters in methods?
Let's take this method as an example
form_input($type, $name, $value = null, $class = null, $placeholder = null)
$type and $name are mandatory, while $value, $class, and $placeholders are not even required. However, if we want to add placeholder, we cannot just put our placeholder parameter after the $name parameter, instead we need to place an empty values for the $value and $class.
echo $form_create->form_input('text', 'name', '', '', 'type your placeholder here');
I hope this class will help you in your future project.
Disclaimer!
I sincerely apologize for using the $class as a parameter name in some of the methods, I did not realize until today that it is one of the reserved keyword in PHP of which I should not have used it even in naming my variables.
I also posted this class on my github gist.