-
Notifications
You must be signed in to change notification settings - Fork 0
/
alternatif.php
185 lines (159 loc) · 6.57 KB
/
alternatif.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
<?php
require './includes/config.php';
if (!isset($_SESSION['admin'])) {
header('Location: login.php');
exit;
}
if (!isset($_SESSION['id'])) {
header('Location: informasi-pemilihan.php');
exit;
} else {
$req = $dbc->prepare("SELECT * FROM pemilihan WHERE id = ?");
$req->bindParam(1, $_SESSION['id']);
$req->execute();
$pemilihan = $req->fetch();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
var_dump($_POST);
$errAlternatif = false;
$errAlternatifData = array();
for ($i = 0; $i < count($_POST['alternatif']); $i++) {
if (!preg_match ('/^[A-Z \'.-]{1,255}$/i', $_POST['alternatif'][$i])) {
$errAlternatifData[] = $i;
}
}
if (!empty($errAlternatifData)) {
$errAlternatif = true;
}
if (!$errAlternatif) {
unset($_SESSION['alternatif']);
unset($_SESSION['errAlternatif']);
unset($_SESSION['errAlternatifData']);
$status = "alternatif";
try {
$dbc->beginTransaction();
$dbc->exec("DELETE FROM alternatif WHERE id_pemilihan = $_SESSION[id]");
$req = $dbc->prepare("INSERT INTO alternatif VALUES(:id, :alternatif)");
$req->bindValue(':id', $_SESSION['id']);
for ($i = 0; $i<count($_POST['alternatif']); $i++) {
$req->bindValue(':alternatif', $_POST['alternatif'][$i]);
$req->execute();
}
$req = $dbc->prepare("UPDATE pemilihan SET status = ? WHERE id = ?");
$req->bindParam(1, $status);
$req->bindParam(2, $_SESSION['id']);
$req->execute();
$dbc->commit();
$_SESSION['alternatif'] = true;
header('Location: nilai-alternatif.php');
exit;
} catch (PDOException $e) {
$dbc->rollback();
}
} else {
$_SESSION['alternatif'] = $_POST['alternatif'];
$_SESSION['errAlternatif'] = $errAlternatif;
$_SESSION['errAlternatifData'] = $errAlternatifData;
header('Location: alternatif.php');
}
} else {
$req = $dbc->prepare("SELECT * FROM alternatif WHERE id_pemilihan = ?");
$req->bindParam(1, $_SESSION['id']);
$req->execute();
$alternatif = $req->fetchAll();
}
$page_title = 'Alternatif';
include './includes/header.php';
?>
<div class="col-md-6 col-md-offset-3 bg-primary">
<div class="page-header text-center">
<h1>Pemilihan Penerima BLT</h1>
<h4><?php echo $pemilihan['keterangan']; ?></h4>
</div>
<h3>Alternatif</h3>
<form method="post">
<div id="alternatif">
<?php
if (isset($_SESSION['errAlternatif']) && $_SESSION['errAlternatif']) {
echo '<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Alternatif</strong> tidak boleh kosong.
</div>';
for ($i = 0; $i < count($_SESSION['alternatif']); $i++) {
if (in_array($i, $_SESSION['errAlternatifData'])) {
echo '<div class="form-group has-error">
<input list="alternatif_list" type="text" class="form-control inputan" name="alternatif[]" placeholder="Alternatif">
</div>';
} else {
echo '<div class="form-group">
<input list="alternatif_list" type="text" class="form-control" name="alternatif[]" placeholder="Alternatif" value="'.$_SESSION['alternatif'][$i].'">
</div>';
}
}
} else if (isset($alternatif) && $alternatif) {
for ($i = 0; $i < count($alternatif); $i++) {
echo '<div class="form-group">
<input list="alternatif_list" type="text" class="form-control" name="alternatif[]" placeholder="Alternatif" value="'.$alternatif[$i]['alternatif'].'">
</div>';
}
} else {
echo '<div class="form-group">
<input list="alternatif_list" type="text" class="form-control inputan" name="alternatif[]" placeholder="Alternatif">
</div>
<div class="form-group">
<input list="alternatif_list" type="text" class="form-control inputan" name="alternatif[]" placeholder="Alternatif">
</div>';
}
?>
<datalist id="alternatif_list"></datalist>
</div>
<div class="text-right">
<button class="btn btn-success" id="tambah_alternatif">Tambah</button>
<button class="btn btn-danger" id="hapus_alternatif">Hapus</button>
</div>
<br/>
<div class="row">
<div class="col-md-6 text-left">
<a class="btn btn-success" href="informasi-pemilihan.php">« Pemilihan</a>
</div>
<div class="col-md-6 text-right">
<button type="submit" class="btn btn-danger">Nilai »</button>
</div>
</div>
</form>
</div>
<script>
$(function() {
$('.inputan').on('input', () => {
var cari = $(this).val()
// console.log(cari)
$('#alternatif_list').html('')
$.ajax({
url: `http://localhost/spk/ajax.php?table=masyarakat&id_dusun=<?= $_GET['id'] ?>&kolom=nama&cari=${cari}`,
success: function(result){
listDusun = JSON.parse(result)
// console.log(listDusun)
listDusun.forEach((i) => {
$('#alternatif_list').append(`<option value="${i.nama}">`)
})
}
})
})
$('#tambah_alternatif').click(function(e) {
e.preventDefault();
$('#alternatif').append('<div class="form-group"><input list="alternatif_list" type="text" class="form-control inputan" name="alternatif[]" placeholder="Alternatif"></div>');
});
$('#hapus_alternatif').click(function(e) {
e.preventDefault();
var banyakAlternatif = $('#alternatif .form-group').length;
if (banyakAlternatif > 2) {
$('#alternatif .form-group:last-child').remove();
} else {
alert("Minimal harus terdapat dua alternatif");
}
});
});
</script>
<?php
include './includes/footer.php';
?>