-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.php
85 lines (78 loc) · 2.82 KB
/
form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
class Form
{
private $action;
private $method;
private $data;
private $allowedTypes = array('checkbox', 'radio');
public function __construct($action="#", $method="post" , $data=array())
{
$this->action = $action;
$this->method = $method;
$this->data = $data;
}
public function addInput($type="text", $name="test", $label="test", $qst="what?"){
if(array_key_exists($name, $this->data) === true){
return;
}
$con = in_array($type, $this->allowedTypes);
if (!$con){
$newInput = array("type" => $type, "label" => $label);
} else {
$newInput = array("type" => $type, "label" => $label, "qst" => $qst);
}
$this->data[$name] = $newInput;
}
public function getHtml(){
$result = '<form action="'. $this->action . '" method = "' . $this->method . '">';
foreach($this->data as $name => $inputElement){
$con = in_array($inputElement["type"], $this->allowedTypes);
// echo "condition is $con<br>";
if (!$con) {
$result .= '<p>'.
$inputElement["label"] . ' : <input type="' . $inputElement["type"] . '" name="'. $name . '">'
.'</p>';
} else {
$result .= '<h3>'.$inputElement["qst"].'</h3>';
foreach($inputElement["label"] as $e) {
$result .= '<input type="' . $inputElement["type"] . '" name="'. $name . '">'.
'<label for='.$name.'>'.$e.'</label><br>';
}
}
}
$result .= '<button type="submit">Submit</button>';
$result .= '</form>';
return $result;
}
}
$data = array(
"username" => array("type" => "text" , "label" => "your username"),
"password" => array("type" => "password", "label" => "your password"),
"email" => array("type" => "email" , "label" => "your email" ),
"chbox[]" => array('type' => "checkbox", "label" => ['v1', 'v2', 'v3'], "qst" => "What is?"),
"r" => array('type' => "radio", "label" => ['v1', 'v2', 'v3'], "qst" => "What is?"),
) ;
$form2 = new Form("#", "get", $data);
$form = new Form();
?>
<!DOCTYPE html>
<html>
<head>
<title>Form POO</title>
</head>
<body>
<h1>First Form: Adding inputs every time:</h1>
<?php
$form->addInput("text", "username", "your username");
$form->addInput("password", "password", "your password");
$form->addInput("checkbox", "chbox[]", ["v1", "v2"], "What is?");
$form->addInput("checkbox", "chbox[]", ["v1", "v2"], "Where is?"); // It will no work because already exist !
$form->addInput("radio", "r", ["v1", "v2"], "When ?");
echo $form->getHtml();
?>
<h1>Second Form: Directly from Data</h1>
<?php
echo $form2->getHtml();
?>
</body>
</html>