survey_seahorse

Software Engineering Project - Fall 2018
Log | Files | Refs | README

submit-survey.php (4550B)


      1 <?php
      2 include_once 'session.php';
      3 include_once '../config.php';
      4 
      5 if (isset($_POST['answers'])) {
      6     $answers = array();
      7     $answers = $_POST['answers'];
      8     $user = $_SESSION['uid'];
      9     $survey_id = $_SESSION['sid'];
     10 
     11     echo '<link rel="stylesheet" href="style.css">';
     12     echo  '<div class="submit-survey">';
     13 
     14     if (isset($_POST['numeric'])) {
     15         $type = 48;
     16         for($counter = 0; $counter < sizeof($answers); $counter++) {
     17             $questionNum = $counter+1;
     18             $q = $answers[$counter];
     19             if ($_SESSION['expired'] != 1 && $_SESSION['taken'] != 1) {
     20                 $sql = "INSERT INTO answer_numeric (user_id, survey_id, question_number, answer)
     21                     VALUES ('$user', '$survey_id', $counter + 1, $q);";
     22                 mysqli_query($connect, $sql) or die(mysqli_error($connect));
     23             }
     24 
     25             $getsql = "SELECT question_content from question where survey_id=$survey_id and question_number=$counter+1;";
     26             $result = mysqli_query($connect, $getsql);
     27             $question = mysqli_fetch_assoc($result);
     28 
     29             $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;";
     30             $ansresult = mysqli_query($connect, $getAnswer);
     31             $ans = mysqli_fetch_assoc($ansresult); 
     32             echo "For Question #", $counter+1, ": ", $question['question_content'], ", you answered: ", $answers[$counter], "<br>";
     33             echo "Most people answered: ", $ans['answer'], "<br>";
     34             //echo "<a href='query.php?question=$questionNum&sid=$survey_id'> More Details </a> <br> <br>";
     35             echo "<a href='script/chart.php?question=$questionNum&sid=$survey_id&type=$type'> More Details </a> <br> <br>";
     36 
     37         }
     38         echo "<a href='../index.php'> Home </a>";
     39         exit();
     40     } elseif (isset($_POST['bool'])) {
     41         for($counter = 0; $counter < sizeof($answers); $counter++) {
     42             $type = 49;
     43             $questionNum = $counter+1;
     44             $q = $answers[$counter];
     45             if ($_SESSION['expired'] != 1 && $_SESSION['taken'] != 1) {
     46                 $sql = "INSERT INTO answer_bool (user_id, survey_id, question_number, answer)
     47                     VALUES ('$user', '$survey_id', $counter + 1, '$q');";
     48                 mysqli_query($connect, $sql) or die(mysqli_error($connect));
     49             }
     50 
     51             $getsql = "SELECT question_content from question where survey_id=$survey_id and question_number=$counter+1;";
     52             $result = mysqli_query($connect, $getsql);
     53             $question = mysqli_fetch_assoc($result);
     54 
     55             $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;";
     56             $ansresult = mysqli_query($connect, $getAnswer);
     57             $ans = mysqli_fetch_assoc($ansresult); 
     58             echo "For Question #", $counter+1, ": ", $question['question_content'], ", you answered: ", $answers[$counter], "<br>";
     59             echo "Most people answered: ", $ans['answer'], "<br>";
     60             //echo "<a href='query.php?question=$questionNum&sid=$survey_id'> More Details </a> <br> <br>";
     61             echo "<a href='script/chart.php?question=$questionNum&sid=$survey_id&type=$type'> More Details </a> <br> <br>";
     62 
     63         }
     64         echo "<a href='../index.php'> Home </a>";
     65         exit();
     66 
     67     } elseif(isset($_POST['text'])) {
     68         for($counter = 0; $counter < sizeof($answers); $counter++)
     69         {
     70             $questionNum = $counter+1;
     71             $q = $answers[$counter];
     72             if ($_SESSION['expired'] != 1 && $_SESSION['taken'] != 1) {
     73                 $sql = "INSERT INTO answer_text (user_id, survey_id, question_number, answer)
     74                     VALUES ('$user', '$survey_id', $counter + 1, $q);";
     75                 mysqli_query($connect, $sql) or die(mysqli_error($connect));
     76             }
     77 
     78             $getsql = "SELECT question_content from question where survey_id=$survey_id and question_number=$counter+1;";
     79             $result = mysqli_query($connect, $getsql);
     80             $question = mysqli_fetch_assoc($result);
     81 
     82             echo "For Question #", $counter+1, ": ", $question['question_content'], ", you answered: ", $answers[$counter], "<br>";
     83         }
     84         unset($_SESSION['expired']);
     85         echo "<a href='../index.php'> Home </a>";
     86         exit();
     87 
     88     }
     89 } else {
     90     header("Location: res/nope.php");
     91     exit();
     92 }
     93 ?>