Hello,
I’ve started to use OOP in the real world now after learning it over the last couple of weeks and created a simple html form with css as shown here.
I believe If I nail a basic form I think I can start using more advanced things like making the form post to a database. Now I think i’ve got on OK with it, but have a few questions please:
<?php
Class FormElements {
private $wrapper;
private $form_class;
private $form_action;
private $form_method;
private $form_legend;
private $input_label;
private $input_type;
private $input_name;
//* 1) CONSTRUCT - the start of my CSS wrapper
function __construct($wrapper='') {
$this->wrapper = $wrapper;
}
//* 2) SETTER - attributes for the form
function setAttributes($form_class,$form_action,$form_method) {
$this->form_class = $form_class;
$this->form_action = $form_action;
$this->form_method = $form_method;
}
//* 3) SETTER - a title to the form in the legend
function setLegend($form_legend) {
$this->form_legend = $form_legend;
}
//* 3) SETTER - the data to create an input box
function setInputBoxes($input_label,$input_type,$input_name) {
$this->input_label = $input_label;
$this->input_type = $input_type;
$this->input_name = $input_name;
}
//* 4) GETTER - the whole form
function getWholeform() {
$output = '<div id="'.$this->wrapper.'">
<form class="'.$this->form_class.'" action="'.$this->form_action.'" method="'.$this->form_method.'">
<fieldset>
<legend>'.$this->form_legend.'</legend>
<p>
<label for="firstname">'.$this->input_label.':</label>
<input type="'.$this->input_type.'" name="'.$this->input_name.'">
</p>';
return $output;
}
//* 5) DESTRUCT - with the closing tags needed for the above form
function __destruct() {
$output = '</div>
</fieldset>
</form>';
return $output;
}
}
//*********** Create the instance *********** //
$form = new FormElements("wrappername");
$form->setAttributes("cssform","nextpage.php","post");
$form->setLegend("This is the title of the form");
$form->setInputBoxes("Username","text","username");
echo $form->getWholeform();
?>
Questions:
-
This look ok? Nothing silly i’ve done obvious?
-
As you can see i’ve created a DESTRUCT method to close the wrapper on the form, but it’s never called called in the html of the page created. How can I encorporate that into my code so it’s outputted after the method getWholeform() is called?
-
In my code I simply output ONE input box in my getWholeform() method, say I wanted three input boxes like so:
$form->getInputBoxes("Username","text","username");
$form->getInputBoxes("password","text","username");
$form->getInputBoxes("secret","text","username");
//...and keep on adding
and wanted to possibly keep on adding input boxes How could I do this?
-
from a purely aesthetic point of view, if you view my Source Code on my page everything is all over the place. How can I tidy-up the method getWholeform() to make the html look cleaner?
-
Is creating a form using OOP overkil and I should just use OOP to add data & request it from the database?
Many Thanks