-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubmit_response.php
More file actions
101 lines (84 loc) · 3.26 KB
/
submit_response.php
File metadata and controls
101 lines (84 loc) · 3.26 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
<?php
if(!isset($_SESSION)) {
session_start();
}
require('connect-db.php');
global $db;
$valid_form = formValid($_POST);
if($valid_form[0] == "true") {
// Add the survey id to the session variable 'completed_surveys'
$survey_id = $_POST['survey_id'];
array_push($_SESSION['completed_surveys'], $survey_id);
foreach($_POST as $index => $value) {
// Split the name of the input field at "-" into an array
$split_name = explode("-", $index);
if($split_name[0] == "question") {
foreach($_POST['question-'.$split_name[1]] as $answer_index => $answer_value) {
$question_id = $split_name[1];
$question_query = "SELECT * FROM questions WHERE id=:question_id";
$get_question = $db->prepare($question_query);
$get_question->bindValue(':question_id', $question_id);
$get_question->execute();
$question_type = $get_question->fetchColumn(2);
if($question_type == "single" || $question_type == "multiple") {
$answer_id = $answer_value;
$number = NULL;
$text = NULL;
}
else if($question_type == "number") {
$number = $answer_value;
$answer_id = NULL;
$text = NULL;
}
else { // Text entry
$text = $answer_value;
$answer_id = NULL;
$number = NULL;
}
addResponse($question_id, $answer_id, $number, $text);
}
}
}
header("Location: index.php");
}
else {
// Show javascript alert and automatically reload new_survey form when alert is clicked
echo "<script type='text/javascript'>if(!alert('$valid_form[1]')){window.history.back()};</script>";
}
// Return true if all questions are answered, return false with error message if not
function formValid($inputs) {
global $db;
foreach($inputs as $index => $value) {
// Split the name of the input field at "-" into an array
$split_name = explode("-", $index);
if($split_name[0] == "question") {
foreach($inputs['question-'.$split_name[1]] as $answer_index => $answer_value) {
$question_id = $split_name[1];
$question_query = "SELECT * FROM questions WHERE id=:question_id";
$get_question = $db->prepare($question_query);
$get_question->bindValue(':question_id', $question_id);
$get_question->execute();
$question_type = $get_question->fetchColumn(2);
if($question_type == "single" || $question_type == "number" || $question_type == "text") {
if(empty($answer_value)) {
return array("false", "Please enter a response for all questions");
}
}
}
}
}
return array("true");
}
// Add a row to the responses table in the database
function addResponse($question_id, $answer_id, $number, $text) {
global $db;
$query = "INSERT INTO responses (question_id, answer_id, number_response, text_response) VALUES (:question_id, :answer_id, :number_response, :text_response)";
$statement = $db->prepare($query);
$statement->bindValue(':question_id', $question_id);
$statement->bindValue(':answer_id', $answer_id);
$statement->bindValue(':number_response', $number);
$statement->bindValue(':text_response', $text);
$statement->execute();
$statement->closeCursor();
}
?>