-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcreateQuestion.php
More file actions
202 lines (180 loc) · 7.94 KB
/
createQuestion.php
File metadata and controls
202 lines (180 loc) · 7.94 KB
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
201
202
<?php $page_title = 'Quiz Master > Create Question'; ?>
<?php
require 'bin/functions.php';
require 'db_configuration.php';
include('header.php');
$page="questions_list.php";
verifyLogin($page);
?>
<?php
$mysqli = NEW MySQLi('localhost','root','','quiz_master');
$resultset = $mysqli->query("SELECT DISTINCT topic FROM topics ORDER BY topic ASC");
?>
<link href="css/form.css" rel="stylesheet">
<style>#title {text-align: center; color: darkgoldenrod;}
.hidden {display: none;}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<!--Check the CeremonyCreated and if Failed, display the error message-->
<?php
//if no image is selected, display error message
if(isset($_GET['createQuestion'])) {
if($_GET["createQuestion"] == "noFileSelected"){
echo '<br><h3 align="center" class="bg-danger">ERROR - No image file selected!</h3>';
}
}
if(isset($_GET['createQuestion'])){
if($_GET["createQuestion"] == "fileRealFailed"){
echo '<br><h3 align="center" class="bg-danger">FAILURE - Your image is not real, Please Try Again!</h3>';
}
}
if(isset($_GET['createQuestion'])){
if($_GET["createQuestion"] == "answerFailed"){
echo '<br><h3 align="center" class="bg-danger">FAILURE - Your answer was not one of the choices, Please Try Again!</h3>';
}
}
if(isset($_GET['createQuestion'])){
if($_GET["createQuestion"] == "fileTypeFailed"){
echo '<br><h3 align="center" class="bg-danger">FAILURE - Your image is not a valid image type (jpg,jpeg,png,gif), Please Try Again!</h3>';
}
}
if(isset($_GET['createQuestion'])){
if($_GET["createQuestion"] == "fileExistFailed") {
echo '<br><h3 align="center" class="bg-danger">FAILURE - Your image already exist, Please Try Again!</h3>';
}
}
?>
<form action="createTheQuestion.php" method="POST" enctype="multipart/form-data">
<br>
<h3 id="title">Create A Question</h3> <br>
<table>
<tr>
<td style="width:100px">Topic:</td>
<td><select name="topic">
<?php
while($rows = $resultset->fetch_assoc()){
$topic=$rows['topic'];
echo"<option Value='$topic'>$topic</option>";}?>
</select></td>
</tr>
<tr>
<td style="width:100px">Question:</td>
<td><input type="text" name="question" maxlength="100" size="50" required title="Please enter a question."></td>
</tr>
<tr>
<td style="width:100px">Choice 1:</td>
<td><input type="text" name="choice_1" maxlength="50" size="50" required title="Please enter answer 1."></td>
</tr>
<tr>
<td style="width:100px">Choice 2:</td>
<td><input type="text" name="choice_2" maxlength="50" size="50" required title="Please enter answer 2."></td>
</tr>
<tr>
<td style="width:100px">Choice 3:</td>
<td><input type="text" name="choice_3" maxlength="50" size="50" required title="Please enter answer 3."></td>
</tr>
<tr>
<td style="width:100px">Choice 4:</td>
<td><input type="text" name="choice_4" maxlength="50" size="50" required title="Please enter answer 4."></td>
</tr>
<tr>
<td style="width:100px">Answer:</td>
<td><input type="text" name="answer" maxlength="50" size="50" required title="Please enter the answer."></td>
</tr>
<tr>
<td style="width:100px">Image:</td>
<td><input type="file" name="fileToUpload" id="fileToUpload" maxlength="50" size="50" title="Please enter the Image Name."></td>
</tr>
<tr>
<input type="hidden" name="keyword_count" id="keyword_count" value = 0>
</tr>
</table>
<!-- Add keyword textboxes | No require to add keywords -->
<tr>
<td colspan="2">
<div id="keywordContainer"></div>
<button type="button" id="addKeyword">Add Keyword</button>
<button type="button" class="removeKeyword hidden">Remove Keyword</button>
</td>
</tr>
<br><br>
<div align="center" class="text-left">
<button type="submit" name="submit" class="btn btn-primary btn-md align-items-center">Create Question</button>
</div>
<br> <br>
</form>
</div>
<!-- Add JS for adding and removing keywords -->
<!-- Source used: https://stackoverflow.com/questions/60008639/add-input-fields-on-button-click-with-data-from-php -->
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> -->
<!-- .hidden {display: none;} -->
<!--Source used to avoid duplication of keywords: https://stackoverflow.com/questions/34388638/how-to-avoid-duplicate-options-from-dropdown-array-in-jquery -->
<script>
//document.ready function loads up the structure of the web site before proceeding with the javascript
$(document).ready(function() {
var i = 1;
var selectedKeywords = []; // Array to store selected keywords to prevent duplicate selection of keywords
//New keyword added to the keyword container div, i variable will start at 1
$('#addKeyword').click(function() {
var keywordDropdown = '<div id="row' + i + '"><label for="keyword_' + i + '">Keyword ' + i + '</label>' +
'<select name="keyword[]" required>' +
'<option value="">Select a keyword</option>';
<?php
//Keywords are stored in this variable to be later displayed on the dropdown box
$keywordResultSet = $mysqli->query("SELECT DISTINCT keyword FROM keywords ORDER BY keyword ASC");
//loop thru every single keyword row and displaying it
while ($keywordRow = $keywordResultSet->fetch_assoc()) {
$keyword = $keywordRow['keyword']; //Hashmap or dictionary
echo "if (!selectedKeywords.includes('$keyword')) {";
echo "keywordDropdown += '<option value=\'$keyword\'>$keyword</option>';";
echo "}";
}
?>
keywordDropdown += '</select></div>';
$('#keywordContainer').append(keywordDropdown);
i++;
//Reveals the remove keyword button when add a keyword
$('.removeKeyword').removeClass('hidden');
updateSelectedKeywords(); // Update the selectedKeywords array
});
//Removes the keyword dropdown when clicking on this button
$(document).on('click', '.removeKeyword', function() {
var button_id = $(this).attr("id");
i--;
$('#row' + $('#keywordContainer div').length).remove();
if (i <= 1) {
$('.removeKeyword').addClass('hidden');
}
updateSelectedKeywords();
});
//Updates the selectedKeywords array with the newly selected keywords
function updateSelectedKeywords() {
selectedKeywords = []; // Reset the array
var keywordCount = 0;
$('select[name^="keyword"]').each(function() {
var selectedKeyword = $(this).val();
if (selectedKeyword !== '') {
selectedKeywords.push(selectedKeyword);
keywordCount++;
}
});
$('#keyword_count').val(keywordCount);
removeSelectedKeywords(); // Call the function to remove the selected keywords
}
//remove/hide selected keywords from drop down box
function removeSelectedKeywords() {
$('select[name^="keyword"]').each(function() {
var currentSelect = $(this);
currentSelect.find('option').show(); // Show all options before removing
selectedKeywords.forEach(function(keyword) {
currentSelect.find('option[value="' + keyword + '"]').hide();
});
});
}
});
</script>
<!-- <div id="keywordContainer"></div>
<button type="button" id="addKeyword">Add Keyword</button>
<button type="button" class="removeKeyword hidden">Remove Keyword</button>
</div> -->