-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.php
100 lines (84 loc) · 1.97 KB
/
example.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example</title>
<style type="text/css">
.data {margin:1em 0;padding:1em;background-color:#eee;border:1px solid #ddd}
.error {border:1px solid red}
.error .message {color:red}
</style>
</head>
<body>
<?php
require_once( 'html5.php' );
$content = "<h2>Dynamically create an HTML5 form</h2>";
$user_data = '{
"first_name":{
"type":"text",
"value":"Joe",
"required":1
},
"last_name":{
"type":"text",
"value":"Joe",
"required":1
},
"email":{
"type":"text",
"value":"joe@smith",
"required":1
},
"username":{
"type":"text",
"value":"",
"required":1
},
"bio":{
"type":"textarea",
"value":"Joe",
"required":0
}
}';
$joe = json_decode( $user_data, true );
// Create a new form container
$form = new Form( $_SERVER['PHP_SELF'] );
// Add fields manually using the tag method
$form->add( $form->tag( 'input', array( 'type' => 'hidden', 'name' => 'registration', 'value' => 'user' ) ) );
foreach ( $joe as $segment['name'] => $segment )
{
$options = array();
$validations = array();
$errors = array();
$options['show_errors'] = true;
if ( $segment['required'] == 1 )
{
$validations[] = 'presence_of';
}
if ( $segment['name'] == 'email' )
{
$validations[] = 'email';
}
$field = new Form_field( $segment['type'], $segment['name'], $segment['value'], $options, $validations );
$errors = $field->process_validations();
$form->add( $field->render( $options ) );
}
// Manually add a submit button
$button = $form->tag( 'input', array( 'type' => 'submit', 'value' => 'Send' ) );
// Attach the button to the form
$form->add( $form->tag( 'fieldset', array( 'class' => 'actions' ), $button ) );
// Finally, render our completed form object and attach it to the content output
$content .= $form->render();
echo $content;
if ( $_POST )
{
print "<div class=\"data\">";
print "<h3>POST Data</h3>";
print "<pre>";
print_r( $_POST );
print "</pre>";
print "</div>";
}
?>
</body>
</html>