-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoll.php
84 lines (75 loc) · 2.22 KB
/
poll.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
<?php
global $url, $connection;
if (empty($url['params'][0])) {
redirect('404');
}
$query = $connection->prepare("SELECT `rowid`, * FROM `polls` WHERE `slug` = :slug LIMIT 1;");
if (!$query) {
redirect("error/database");
}
$params = array(
':slug' => $url['params'][0]
);
if (!$query->execute($params)) {
redirect("error/database");
}
$poll = $query->fetchObject();
$expired = $poll->expires && (strtotime($poll->expires) - strtotime() <= 0);
if (!$poll->enabled || $expired) {
$msg = $expired ? 'expired' : 'off';
redirect("error/$msg");
}
// get existing votes
$votes = array();
if (isset($_COOKIE['votes'])) {
$votes = unserialize($_COOKIE['votes']);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>RH Vote!</title>
<link rel="stylesheet" href="<?php echo $url['base']; ?>/css/fonts.css" />
<link rel="stylesheet" href="<?php echo $url['base']; ?>/css/styles.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('form').submit(function() {
var form = $(this);
$.ajax({
url: $(this).prop('action'),
type: $(this).prop('method'),
data: $(this).serialize(),
dataType: 'json'
}).done(function(data) {
form.find('[type="submit"]').prop('disabled', true)
});
return false;
});
});
</script>
</head>
<body>
<section>
<header class="page-header">
<img src="<?php echo $url['base']; ?>/img/header.jpg" />
</header>
<?php
$query = $connection->prepare("SELECT `rowid`, * FROM `questions` WHERE `poll_id` = :poll_id ORDER BY `order` ASC;");
if ($query->execute(array(':poll_id' => $poll->rowid))):
while ($question = $query->fetchObject()): ?>
<form action="<?php echo $url['base']; ?>/vote" method="post">
<div class="poll-question"><?php echo $question->question; ?></div>
<input type="hidden" name="question" value="<?php echo $question->rowid; ?>" />
<?php if (!isset($votes[$question->rowid])): ?>
<button class="btn" type="submit">Yes</button>
<?php else: ?>
<button class="btn" type="submit" disabled>Yes</button>
<?php endif; ?>
</form>
<?php endwhile; ?>
<?php endif; ?>
</section>
</body>
</html>