-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathshort_story_dump.php
160 lines (103 loc) · 3.4 KB
/
short_story_dump.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
<?php
require("../../../config.php");
session_start();
$database = "if16_brigitta";
$storyError = "";
$story = "";
if (empty ($_POST["story"])) {
$storyError = "you have to write something";
}
function saveStory($author, $story) {
$mysqli = new mysqli($GLOBALS["serverHost"], $GLOBALS["serverUsername"], $GLOBALS["serverPassword"], $GLOBALS["database"]);
$stmt = $mysqli->prepare("INSERT INTO story_dump (author, story) VALUES(?,?)");
echo $mysqli->error;
$stmt->bind_param("ss", $author, $story);
if ($stmt->execute() ) {
echo "saving success";
} else {
echo "ERROR ".$stmt->error;
}
}
function cleanInput ($input)
{
$input = trim($input);
$input = stripslashes($input);
$input = htmlspecialchars($input);
return $input;
}
if(isset($_POST['author']) &&
isset($_POST['story']) &&
!empty($_POST['story'])
) {
if(empty($_POST["author"])) {
$author = "anonymous";
} else {
$author = cleanInput($_POST["author"]);
}
$story = cleanInput($_POST["story"]);
saveStory($author, $story);
}
function getAllStories() {
$mysqli = new mysqli($GLOBALS["serverHost"], $GLOBALS["serverUsername"], $GLOBALS["serverPassword"], $GLOBALS["database"]);
$stmt = $mysqli->prepare("SELECT id, author, story FROM story_dump ORDER BY id DESC");
$stmt->bind_result($id, $author, $story);
$stmt->execute();
$result = array();
while($stmt->fetch()) {
//echo $note."<br>";
//lilith
$object = new StdClass();
$object->id = $id;
$object->author = $author;
$object->story = $story;
array_push($result, $object);
}
//return $result;
foreach($result as $item) {
// echo '<div style="position:relative;">';
echo '<div style="display:flex;width:33%;margin-bottom:5px;"><div style="background-color:rgba('.rand(0,255).','.rand(0,255).','.rand(0,255).',0.5'.rand(0,1).');float:left;display:flex;justify-content:flex-end;word-wrap:break-word;">';
echo $item->author." wrote: <br><br>";
echo $item->story;
echo '</div></div>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>story_dump</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var text_max = 666;
$('#textarea_feedback').html(text_max + ' characters remaining');
$('#textarea').keyup(function() {
var text_length = $('#textarea').val().length;
var text_remaining = text_max - text_length;
$('#textarea_feedback').html(text_remaining + ' characters remaining');
});
});
</script>
</head>
<body>
<div style="display:block;">
<div style="float:left; width:50%;height:1000px;">
<h1 style="color:#8a0707;">write your deepest darkest secrets here</h1>
<form method="POST">
<label>what would you like to be called?</label>
<br>
<input name="author" type="text" placeholder="anonymous">
<br><br>
<textarea name="story" style="width: 300px; height: 150px;" placeholder="speak your mind.." id="textarea" maxlength="666"></textarea>
<div id="textarea_feedback"></div>
<?php if(isset($_POST['author'])) { echo $storyError; } ?>
<br><br>
<input type="submit" value="submit">
</form>
</div>
<div style="">
<?php getAllStories(); ?>
</div>
</div>
</body>
</html>