Formulier Class
Ik gebruik smarty en ik probeer html en php zo ver mogelijk van elkaar weg te houden. Dit is echter lastig als je veel met formulieren werkt. Daarom heb ik een class gemaakt die ervoor zorgt dat je html en php toch netjes kunt scheiden. (behalve in de class zelf natuurlijk) En kleine uitleg van de functies: new form($name = "Verstuur", $target, $method = "POST", $reset = false) $name: wordt gebruikt als name van de form tag en de value van de submit button. $target: is de action van de form-tag. $method: spreekt voor zich $reset: bepaalt of er een reset button wordt getoond input($type, $name, $value) $type: type van de input, kan zijn -> text,password,radio,hidden,file,select,textarea $name: name en id van de input/select/textarea tag, ook de inhoud van het label. Deze kan spaties bevatten, omdat die voor de id en name worden omgezet in '_' $value: is string of array afhankelijk van het type. (bij radio en select is het een array) De html wordt gestopt in $this->html. Een voorbeeldje:
--------- CSS --------------------
<style type="text/css">
.cssform p {
width: 300px;
clear: left;
padding: 5px 0 8px 0;
padding-left: 155px;
height: 1%;
}
.cssform label {
float: left;
margin-left: -155px;
}
</style>
-------------- PHP -----------------------
<?php
class form
{
var $start;
var $body;
var $end;
var $html;
function __construct($name = "Verstuur", $target, $method = "POST", $reset = false) {
$id = strtolower(str_replace(' ', '_', $name));
$this->start = "<form class='cssform' name='".$id."' action='".$target."' method='".$method."'>";
$this->end .= "<p>";
if ($reset) $this->end .= "<input type='reset' value='Stel opnieuw in' />";
$this->end .= "<input type='submit' name='".$id."' value='".$name."' /></p>";
$this->end .= "</form>";
$this->html = $this->start . $this->end;
}
function input($type, $name, $value) {
$id = strtolower(str_replace(' ', '_', $name));
switch ($type) {
case 'text':
case 'password':
$this->body .= "<p><label for='".$id."'>".$name.": </label>";
$this->body .= "<input type='".$type."' name='".$id."' id='".$id."' value='".$value."' /></p>";
break;
case 'hidden':
$this->body .= "<input type='hidden' name='".$id."' id='".$id."' value='".$value."' />";
break;
case 'checkbox':
$this->body .= "<p>";
$this->body .= "<input type='checkbox' name='".$id."' id='".$id."' value='".$value."' /> ".$value."<br />";
$this->body .= "</p>";
break;
case 'radio':
$this->body .= "<p><label>".$name.": </label>";
foreach ($value as $option) {
$optionId = strtolower(str_replace(' ', '_', $option));
$this->body .= "<input type='radio' name='".$id."' id='".$id."' value='".$optionId."' /> ".$option."<br />";
}
$this->body .= "</p>";
break;
case 'file':
$this->body .= "<p><label for='".$id."'>".$name.": </label>";
$this->body .= "<input type='file' name='".$id."' id='".$id."' value='Kies bestand' /></p>";
break;
case 'select':
$this->body .= "<p><label for='".$id."'>".$name.": </label>";
$this->body .= "<select name='".$id."' id='".$id."'>";
foreach ($value as $option) {
$optionId = strtolower(str_replace(' ', '_', $option));
$this->body .= "<option value='".$optionId."'>".$option."</option>";
}
$this->body .= "</select></p>";
break;
case 'textarea':
$this->body .= "<p><label for='".$id."'>".$name.": </label>";
$this->body .= "<textarea name='".$id."' id='".$id."'>".$value."</textarea></p>";
break;
}
$this->html = $this->start . $this->body . $this->end;
}
}
?>
Reacties
0