-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.php
200 lines (182 loc) · 6.22 KB
/
create.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
// Link to the class files
require_once 'Notes.php';
require_once 'FileNotesRepo.php';
// Checking for blank spaces, html and sql injection
function values_in_fields( $values_entered ) {
$values_entered = trim( $values_entered );
$values_entered = stripslashes( $values_entered );
$values_entered = htmlspecialchars( $values_entered );
return $values_entered;
}
// Checking the required fields
$notesSubject = isset( $_POST['subject'] ) ? values_in_fields( $_POST['subject'] ) : '';
$notesAuthor = isset( $_POST['author'] ) ? values_in_fields( $_POST['author'] ) : '';
$notesBody = isset( $_POST['body'] ) ? values_in_fields( $_POST['body'] ) : '';
$notesPriority = isset( $_POST['priority'] ) ? $_POST['priority'] : '';
// Assigning, declaring variables
$formIsValid = true;
$subjectErr = '';
$authorErr = '';
$bodyErr = '';
$priorityErr = '';
// If fields empty give an error message
if ( empty( $notesSubject ) ){
$formIsValid = false;
$subjectErr = '<span style="color : #f00;"> Subject is required </span>';
}
if ( empty( $notesAuthor ) ){
$formIsValid = false;
$authorErr = '<span style="color : #f00;"> Author is required </span>';
}
if ( empty( $notesBody ) ){
$formIsValid = false;
$bodyErr = '<span style="color : #f00;"> Body is required </span>';
}
if ( empty( $notesPriority ) ){
$formIsValid = false;
$priorityErr = '<span style="color : #f00;"> Priority is required </span>';
}
?>
<!-- Create page -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> Add new note </title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<!-- Logic for the create page -->
<?php if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) : ?>
<?php if ( $formIsValid ) : ?>
<?php
$notesRepo = new \abhijeet\NFS\FileNotesRepo();
$notes = new \abhijeet\NFS\Notes();
$notes->setSubject( $notesSubject );
$notes->setAuthor( $notesAuthor );
$notes->setBody( $notesBody );
$notes->setPriority( $notesPriority );
$notesRepo->saveNotes( $notes );
?>
<div class="container-fluid">
<!-- Review create page -->
<h1> New note created </h1>
<table class="table">
<caption> Review created note </caption>
<tbody>
<tr>
<td width="100px;"><label> Subject: </label></td>
<td><?php print $notesSubject; ?></td>
</tr>
<tr>
<td width="100px;"><label> Author: </label></td>
<td><?php print $notesAuthor; ?></td>
</tr>
<tr>
<td width="100px;"><label> Body: </label></td>
<td><?php print $notesBody; ?></td>
</tr>
<tr>
<td width="100px;"><label> Priority: </label></td>
<td><?php print $notesPriority; ?></td>
</tr>
<tr>
<td colspan="2"><a href="index.php"><input class="btn btn-primary" type="button" value="Go home" /></a></td>
</tr>
</tbody>
</table>
</div>
<?php else : ?>
<div class="container-fluid">
<!-- Create page with error message -->
<h1> Create new note </h1>
<form method="post" action="create.php">
<table class="table">
<caption> Enter values <br /><span style="color: #f00;"> All fields required </span></caption>
<tbody>
<tr>
<td><label> Subject: </label></td>
<td><input size="150" type="text" name="subject" placeholder="Enter subject" value="<?php print $notesSubject; ?>" /><?php print $subjectErr; ?></td>
</tr>
<tr>
<td><label> Author: </label></td>
<td><input size="150" type="text" name="author" placeholder="Enter author's name" value="<?php print $notesAuthor; ?>" /><?php print $authorErr; ?></td>
</tr>
<tr>
<td><label> Body: </label></td>
<td><input size="150" type="text" name="body" placeholder="Enter body" value="<?php print $notesBody; ?>" /><?php print $bodyErr; ?></td>
</tr>
<tr>
<td><label> Priority: </label></td>
<td>
<select name="priority">
<?php $priorityArray = array( 'Low', 'Medium', 'High' ); ?>
<?php
if ( !empty( $notesPriority ) ) {
foreach ( $priorityArray as $value ) {
$selected = '';
if ( $notesPriority == $value ) {
$selected = 'selected';
}
print "<option $selected> $value </option>";
}
} else {
foreach ( $priorityArray as $value ) {
print "<option $selected> $value </option>";
}
}
?>
</select><?php print $priorityErr; ?>
</td>
</tr>
</tbody>
</table>
<input class="btn btn-primary" type="submit" value="Save note" />
<a href="index.php"><input class="btn btn-primary" type="button" value="Go home" /></a>
</form>
</div>
<?php endif; ?>
<?php else : ?>
<div class="container-fluid">
<!-- Create page -->
<h1> Create new note </h1>
<form method="post" action="create.php">
<table class="table">
<caption> Enter values </caption>
<tbody>
<tr>
<td><label> Subject: </label></td>
<td><input size="170" type="text" name="subject" placeholder="Enter subject" /></td>
</tr>
<tr>
<td><label> Author: </label></td>
<td><input size="170" type="text" name="author" placeholder="Enter author's name" /></td>
</tr>
<tr>
<td><label> Body: </label></td>
<td><input size="170" type="text" name="body" placeholder="Enter body" /></td>
</tr>
<tr>
<td><label> Priority: </label></td>
<td><select name="priority">
<option> Low </option>
<option> Medium </option>
<option> High </option>
</select></td>
</tr>
</tbody>
</table>
<input class="btn btn-primary" type="submit" value="Save note" />
<a href="index.php"><input class="btn btn-primary" type="button" value="Go home" /></a>
</form>
</div>
<?php endif; ?>
</body>
</html>