commit b0306ef90d71f9907b5f5c4bfce925242a9539b6
parent 069ad38ff579ec7dbcb1c80a84d547d97167a304
Author: John Kubach <johnkubach@gmail.com>
Date: Wed, 19 Dec 2018 14:20:21 -0500
add yes / no option for surveys
surveys with a yes / no answer can now be taken. answers are stored in database and results can be viewed.
Diffstat:
3 files changed, 148 insertions(+), 61 deletions(-)
diff --git a/website/res/query.php b/website/res/query.php
@@ -3,7 +3,17 @@ include_once '../config.php';
header('Content-Type: application/json');
$question = $_GET['question'];
$sid = $_GET['sid'];
-$query = sprintf("SELECT question_number, answer, count(answer) AS num_ans FROM answer_numeric WHERE (survey_id=$sid AND question_number=$question) GROUP BY answer ORDER BY answer;");
+$type = $_GET['type'];
+
+if ($type == 48) {
+ $table = 'answer_numeric';
+} elseif ($type == 49) {
+ $table = 'answer_bool';
+} else {
+ header("Location: res/nope.php");
+ exit();
+}
+$query = sprintf("SELECT question_number, answer, count(answer) AS num_ans FROM $table WHERE (survey_id=$sid AND question_number=$question) GROUP BY answer ORDER BY answer;");
$result = $connect->query($query);
@@ -15,4 +25,4 @@ foreach ($result as $row) {
$result->close();
$connect->close();
print json_encode($data);
-
+?>
diff --git a/website/res/submit-survey.php b/website/res/submit-survey.php
@@ -5,36 +5,86 @@ ini_set('session.cache_limiter','public');
session_cache_limiter(false);
session_start();
include_once '../config.php';
-$answers = array();
-
-$user = $_SESSION['uid'];
-$survey_id = $_SESSION['sid'];
-$answers = $_POST['answers'];
-
-echo '<link rel="stylesheet" href="style.css">';
-echo '<div class="submit-survey">';
-
-for($counter = 0; $counter < sizeof($answers); $counter++)
-{
- $questionNum = $counter+1;
- $q = $answers[$counter];
- $sql = "INSERT INTO answer_numeric (user_id, survey_id, question_number, answer)
- VALUES ('$user', '$survey_id', $counter + 1, $q);";
- mysqli_query($connect, $sql) or die(mysqli_error($connect));
-
- $getsql = "SELECT question_content from question where survey_id=$survey_id and question_number=$counter+1;";
- $result = mysqli_query($connect, $getsql);
- $question = mysqli_fetch_assoc($result);
-
- $getAnswer = "SELECT answer, count(answer) AS num_ans FROM answer_numeric WHERE (survey_id=$survey_id AND question_number=$counter+1) GROUP BY answer ORDER BY num_ans DESC LIMIT 1;";
- $ansresult = mysqli_query($connect, $getAnswer);
- $ans = mysqli_fetch_assoc($ansresult);
- echo "For Question #", $counter+1, ": ", $question['question_content'], ", you answered: ", $answers[$counter], "<br>";
- echo "Most people answered: ", $ans['answer'], "<br>";
- //echo "<a href='query.php?question=$questionNum&sid=$survey_id'> More Details </a> <br> <br>";
- echo "<a href='script/chart.php?question=$questionNum&sid=$survey_id'> More Details </a> <br> <br>";
+if (isset($_POST['answers'])) {
+ $answers = array();
+ $answers = $_POST['answers'];
+ $user = $_SESSION['uid'];
+ $survey_id = $_SESSION['sid'];
+
+ echo '<link rel="stylesheet" href="style.css">';
+ echo '<div class="submit-survey">';
+
+ if (isset($_POST['numeric'])) {
+ $type = 48;
+ for($counter = 0; $counter < sizeof($answers); $counter++) {
+ $questionNum = $counter+1;
+ $q = $answers[$counter];
+ $sql = "INSERT INTO answer_numeric (user_id, survey_id, question_number, answer)
+ VALUES ('$user', '$survey_id', $counter + 1, $q);";
+ mysqli_query($connect, $sql) or die(mysqli_error($connect));
+
+ $getsql = "SELECT question_content from question where survey_id=$survey_id and question_number=$counter+1;";
+ $result = mysqli_query($connect, $getsql);
+ $question = mysqli_fetch_assoc($result);
+
+ $getAnswer = "SELECT answer, count(answer) AS num_ans FROM answer_numeric WHERE (survey_id=$survey_id AND question_number=$counter+1) GROUP BY answer ORDER BY num_ans DESC LIMIT 1;";
+ $ansresult = mysqli_query($connect, $getAnswer);
+ $ans = mysqli_fetch_assoc($ansresult);
+ echo "For Question #", $counter+1, ": ", $question['question_content'], ", you answered: ", $answers[$counter], "<br>";
+ echo "Most people answered: ", $ans['answer'], "<br>";
+ //echo "<a href='query.php?question=$questionNum&sid=$survey_id'> More Details </a> <br> <br>";
+ echo "<a href='script/chart.php?question=$questionNum&sid=$survey_id&type=$type'> More Details </a> <br> <br>";
+
+ }
+ echo "<a href='../index.php'> Home </a>";
+ exit();
+ } elseif (isset($_POST['bool'])) {
+ for($counter = 0; $counter < sizeof($answers); $counter++) {
+ $type = 49;
+ $questionNum = $counter+1;
+ $q = $answers[$counter];
+ $sql = "INSERT INTO answer_bool (user_id, survey_id, question_number, answer)
+ VALUES ('$user', '$survey_id', $counter + 1, '$q');";
+ mysqli_query($connect, $sql) or die(mysqli_error($connect));
+
+ $getsql = "SELECT question_content from question where survey_id=$survey_id and question_number=$counter+1;";
+ $result = mysqli_query($connect, $getsql);
+ $question = mysqli_fetch_assoc($result);
+
+ $getAnswer = "SELECT answer, count(answer) AS num_ans FROM answer_bool WHERE (survey_id=$survey_id AND question_number=$counter+1) GROUP BY answer ORDER BY num_ans DESC LIMIT 1;";
+ $ansresult = mysqli_query($connect, $getAnswer);
+ $ans = mysqli_fetch_assoc($ansresult);
+ echo "For Question #", $counter+1, ": ", $question['question_content'], ", you answered: ", $answers[$counter], "<br>";
+ echo "Most people answered: ", $ans['answer'], "<br>";
+ //echo "<a href='query.php?question=$questionNum&sid=$survey_id'> More Details </a> <br> <br>";
+ echo "<a href='script/chart.php?question=$questionNum&sid=$survey_id&type=$type'> More Details </a> <br> <br>";
+
+ }
+ echo "<a href='../index.php'> Home </a>";
+ exit();
+
+ } elseif(isset($_POST['text'])) {
+ for($counter = 0; $counter < sizeof($answers); $counter++)
+ {
+ $questionNum = $counter+1;
+ $q = $answers[$counter];
+ $sql = "INSERT INTO answer_text (user_id, survey_id, question_number, answer)
+ VALUES ('$user', '$survey_id', $counter + 1, $q);";
+ mysqli_query($connect, $sql) or die(mysqli_error($connect));
+
+ $getsql = "SELECT question_content from question where survey_id=$survey_id and question_number=$counter+1;";
+ $result = mysqli_query($connect, $getsql);
+ $question = mysqli_fetch_assoc($result);
+
+ echo "For Question #", $counter+1, ": ", $question['question_content'], ", you answered: ", $answers[$counter], "<br>";
+ }
+ echo "<a href='../index.php'> Home </a>";
+ exit();
+
+ }
+} else {
+ header("Location: res/nope.php");
+ exit();
}
- echo "<a href='../index.php'> Home </a>";
-exit();
?>
diff --git a/website/takesurvey.php b/website/takesurvey.php
@@ -1,15 +1,14 @@
<?php
- session_start();
- if(!isset($_SESSION['uid'])){
- header("location: login.php?user=notloggedin");
- }
+session_start();
+if(!isset($_SESSION['uid'])){
+ header("location: login.php?user=notloggedin");
+}
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
ini_set('session.cache_limiter','public');
session_cache_limiter(false);
-session_start();
include_once 'config.php';
include 'res/navbar.php';
@@ -28,13 +27,13 @@ if (empty($code)) {
header("Location: ../findsurvey.php?find=error");
exit();
} elseif (empty($uid)) {
- header("Location: login.php?sid=" . $_GET['sid']);
+ header("Location: login.php?sid=" . $_GET['sid']);
} else {
-// $sql = "SELECT * FROM survey WHERE access_code='$code'";
+ // $sql = "SELECT * FROM survey WHERE access_code='$code'";
$result = mysqli_query($connect, $sql);
$check = mysqli_num_rows($result);
-$block = $_SESSION['blocked'];
+ $block = $_SESSION['blocked'];
if ($check < 1) {
header("Location: ../findsurvey.php?find=error");
@@ -50,31 +49,59 @@ $block = $_SESSION['blocked'];
$sid = $row['survey_id'];
$title = $row['title'];
$number_questions = $row['number_questions'];
+ $type = ord($row['type']);
+ echo $type;
echo "<form method = 'POST' action = 'res/submit-survey.php'>";
echo "<h1> $title </h1> <br>";
- for($counter = 0; $counter < $number_questions; $counter++)
- {
- $sql = "SELECT question_content FROM question WHERE survey_id=$sid and question_number=$counter+1;";
- $result = mysqli_query($connect, $sql);
- $q = mysqli_fetch_assoc($result);
- echo $q['question_content'], "<br>";
- echo " <select name = 'answers[]'>
- <option value='1'>1</option>
- <option value='2'>2</option>
- <option value='3'>3</option>
- <option value='4'>4</option>
- <option selected='selected' value='5'>5</option>
- <option value='6'>6</option>
- <option value='7'>7</option>
- <option value='8'>8</option>
- <option value='9'>9</option>
- <option value='10'>10</option>
- </select> ";
- // echo "<input type='range' name = 'answers[]' min='1' max='10' value='5' step='1' class='slider' id='answerSlide'> <br>";
- echo "<p> <span id='demo'</span> </p>";
+
+ if ($type == 48) {
+ for($counter = 0; $counter < $number_questions; $counter++) {
+ $sql = "SELECT question_content FROM question WHERE survey_id=$sid and question_number=$counter+1;";
+ $result = mysqli_query($connect, $sql);
+ $q = mysqli_fetch_assoc($result);
+ echo $q['question_content'], "<br>";
+ echo " <select name = 'answers[]'>
+ <option value='1'>1</option>
+ <option value='2'>2</option>
+ <option value='3'>3</option>
+ <option value='4'>4</option>
+ <option selected='selected' value='5'>5</option>
+ <option value='6'>6</option>
+ <option value='7'>7</option>
+ <option value='8'>8</option>
+ <option value='9'>9</option>
+ <option value='10'>10</option>
+ </select> ";
+ echo "<p> <span id='demo'</span> </p>";
+ }
+ echo "<button name ='numeric' type = 'submit' value = 'Submit'/>";
+ } elseif ($type == 49) {
+ for($counter = 0; $counter < $number_questions; $counter++) {
+ $sql = "SELECT question_content FROM question WHERE survey_id=$sid and question_number=$counter+1;";
+ $result = mysqli_query($connect, $sql);
+ $q = mysqli_fetch_assoc($result);
+ echo $q['question_content'], "<br>";
+ echo " <select name = 'answers[]'>
+ <option selected = 'selected' value='Yes'>Yes</option>
+ <option value='No'>No</option>
+ </select> ";
+ echo "<p> <span id='demo'</span> </p>";
+ }
+ echo "<button name ='bool' type = 'submit' value = 'Submit'/>";
+ } elseif ($type == 50) {
+ for($counter = 0; $counter < $number_questions; $counter++) {
+ $sql = "SELECT question_content FROM question WHERE survey_id=$sid and question_number=$counter+1;";
+ $result = mysqli_query($connect, $sql);
+ $q = mysqli_fetch_assoc($result);
+ echo $q['question_content'], "<br> <br>";
+ echo " <form name = 'answers[]'>
+ <input type = 'text' placeholder = 'Answer'></input>
+ </form> ";
+ echo "<p> <span id='demo'</span> </p>";
+ }
+ echo "<button name ='text' type = 'submit' value = 'Submit'/>";
}
- echo "<input type = 'submit' value = 'SEND'/>";
echo "</form>";
}
}