-
Notifications
You must be signed in to change notification settings - Fork 3
/
elections.php
77 lines (77 loc) · 2.66 KB
/
elections.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
<html>
<head>
<title>Vit Government Voting (Official)</title>
</head>
<body>
<h1>Voting Form</h1>
<form action="" method="post">
<table id="formstable">
<tr>
<td>Sumit</td>
<td>
<input type="number" name="sumit" id="sumit">
</td>
</tr>
<tr>
<td>Arpit</td>
<td>
<input type="number" name="arpit" id="arpit">
</td>
</tr>
<tr>
<td>Harsha</td>
<td>
<input type="number" name="harsha" id="harsha">
</td>
</tr>
<tr>
<td>Can 4</td>
<td>
<input type="number" name="can4" id="can4">
</td>
</tr>
<tr>
<td><input type="submit" name="vote" value="vote"></td>
<td><input type="submit" name="declare-result" value="declare-result"></td>
</tr>
</table>
</form>
<?php
$names = array("Sumit", "Arpit", "Harsha", "Can4");
$cookie_name = "scores";
if(!isset($_COOKIE[$cookie_name]))
{
$scores = array(0,0,0,0);
setcookie($cookie_name, serialize($scores));
}
else
{
$scores = unserialize($_COOKIE[$cookie_name], ["allowed_classes" => false]);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(isset($_POST['vote']))
{
$scores[0] += intval($_POST["sumit"]);
$scores[1] += intval($_POST["arpit"]);
$scores[2] += intval($_POST["harsha"]);
$scores[3] += intval($_POST["can4"]);
echo "Vote has been casted";
}
if(isset($_POST['declare-result']))
{
$maxvalue = max($scores);
$str = "The candidate(s) with the maximum number of votes are : <br>";
for ($i=0; $i < 4; $i++) {
if($scores[$i] == $maxvalue)
{
$str = $str.$names[$i]." ".$scores[$i]."<br>";
}
}
echo $str;
}
setcookie($cookie_name, serialize($scores));
}
?>
</body>
</html>