-5

i have created a quiz program and i am not able to generate the proper result,even if i select the wrong radio button i get the correct answer and all of my fill i the blanks show correct answer even if i leave them blank.please help me resolve this issue.i need to get this done for my project and i need to get it quick,

 HTML code:
 <?php
 //include auth.php file on all secure pages

 ?>
<html>
<head>
<title>online examination system</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" type="text/css" href="style2.css">    
<meta charset="UTF-8">
<script language="javascript" type="text/javascript" src="libraries/p5.js"></script>
<script language="javascript" src="libraries/p5.dom.js"></script>
<script language="javascript" src="libraries/p5.sound.js"></script>
<script language="javascript" type="text/javascript" src="sketch.js">   </script>
</head>
<body>
<div class=header>
<img class="img-style" src="Online-Examination-System-Banner.jpeg" width="1166px" height="250px">
</div>
<div class="top">
<ul id="navigation">
<li><a href="index.php">HOME</a></li>    
<li><a href="aboutus.php">ABOUT US</a></li>

<li><a href="examination.php">EXAMINATION</a></li>
<li><a href="login.php">LOGIN</a></li>
<li><a href="logout.php">LOGOUT</a></li>

</ul> 
</div>
<p style="font-size:40;color:white;font-weight:bolder;">Welcome  !</p>
<center><p id="timer" style="color:white;font-family:courier;font-size:28px;">______</p></center>
<form action="result.php" method="post" >
<div style="margin-top:10px;margin-right:85px;margin-left:100px;margin- bottom:20px;background:rgba(0,0,0,0.5)">
   <p style="color:white;font-family:courier;font-size:18px">1. Which of the following correctly declares an array?</p>  <br> 
       <input  class="exam-btn" type="radio" name="q1" id="q1-a" value="A" >
           <label for="q1-a" style="color:white;font-family:courier;font-size:18px">int array[10];</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input  class="exam-btn" type="radio" name="q1" id="q1-b" value="B">
           <label style="color:white;font-family:courier;font-size:18px">int array;</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input  class="exam-btn" type="radio" name="q1" id="q1-c" value="C">
           <label style="color:white;font-family:courier;font-size:18px">array{10};</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input  class="exam-btn" type="radio" name="q1" id="q1-d" value="D">
           <label style="color:white;font-family:courier;font-size:18px">array array[10];</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   <br><br><br><br>

   <p style="color:white;font-family:courier;font-size:18px">2. What is the index number of the last element of an array with 9 elements?</p>  <br> 
       <input  class="exam-btn" type="radio" name="q2" id="q2-a" value="A">
           <label style="color:white;font-family:courier;font-size:18px">9</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input  class="exam-btn" type="radio" name="q2" id="q2-b" value="B" >
           <label style="color:white;font-family:courier;font-size:18px">8</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input  class="exam-btn" type="radio" name="q2" id="q2-c" value="C">
           <label style="color:white;font-family:courier;font-size:18px">0</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input  class="exam-btn" type="radio" name="q2" id="q2-d" value="D">
           <label style="color:white;font-family:courier;font-size:18px">programmer defined</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   <br><br><br><br>

<p style="color:white;font-family:courier;font-size:18px">3. Which of the following accesses the seventh element stored in array?</p>  <br> 
       <input  class="exam-btn" type="radio" name="q3" id="q3-a" value="A" >
           <label style="color:white;font-family:courier;font-size:18px">array[6];</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input  class="exam-btn" type="radio" name="q3" id="q3-b" value="B" >
           <label style="color:white;font-family:courier;font-size:18px">array[7];</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input  class="exam-btn" type="radio" name="q3" id="q3-c" value="C" >
           <label style="color:white;font-family:courier;font-size:18px">array(7);</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input  class="exam-btn" type="radio" name="q3" id="q3-d" value="D" >
           <label style="color:white;font-family:courier;font-size:18px">array;</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   <br><br><br><br>

<p style="color:white;font-family:courier;font-size:18px">4. Which of the following gives the memory address of the first element in array?</p>  <br> 
       <input  class="exam-btn" type="radio" name="q4" id="q4-a" value="A" >
           <label style="color:white;font-family:courier;font-size:18px">array[0];</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input  class="exam-btn" type="radio" name="q4" id="q4-b" value="B" >
           <label style="color:white;font-family:courier;font-size:18px">array[1];</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input  class="exam-btn" type="radio" name="q4" id="q4-c" value="C" >
           <label style="color:white;font-family:courier;font-size:18px">array[2];</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input  class="exam-btn" type="radio" name="q4" id="q4-d" value="D" >
           <label style="color:white;font-family:courier;font-size:18px">none</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   <br><br><br><br>

<p style="color:white;font-family:courier;font-size:18px">5. What will be the output of this program?</p>  <br> 
   <pre style="color:white;font-family:courier;font-size:18px">
           #include <stdio.h>

    using namespace std;

    int array1[] = {1200, 200, 2300, 1230, 1543};

    int array2[] = {12, 14, 16, 18, 20};

    int temp, result = 0;

    int main()

    {

        for (temp = 0; temp < 5; temp++) 

        {

            result += array1[temp];

        }

        for (temp = 0; temp < 4; temp++)

        {

            result += array2[temp];

        }

        cout << result;

        return 0;

    }

   </pre>
   </pre>
   <input  class="exam-btn" type="radio" name="q5" id="q5-a" value="A" >
           <label style="color:white;font-family:courier;font-size:18px">6553</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input  class="exam-btn" type="radio" name="q5" id="q5-b" value="B" >
           <label style="color:white;font-family:courier;font-size:18px">6533</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input  class="exam-btn" type="radio" name="q5" id="q5-c" value="C" >
           <label style="color:white;font-family:courier;font-size:18px">6522</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input  class="exam-btn" type="radio" name="q5" id="q5-d" value="D" >
           <label style="color:white;font-family:courier;font-size:18px">12200</label>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   <br><br><br><br>

<! 1.  token                  2.  sensitive              3.  identifiers

4.  octal                   5.  \0n  --> 
  <p style="color:white;font-family:courier;font-size:18px">6.The smallest individual unit in a program is known as a …………………… </p>  <br> 
       <p style="color:white;font-family:courier;font-size:18px">ans</p><input  class="exam-btn" type="text" name="q6" >
<br><br><br><br>

  <p style="color:white;font-family:courier;font-size:18px">7.C++ Language is case …………………. </p>  <br> 
       <p style="color:white;font-family:courier;font-size:18px">ans</p><input  class="exam-btn" type="text" name="q7" >
<br><br><br><br>

  <p style="color:white;font-family:courier;font-size:18px">8.An ………………… is a long sequence of letters and digits. </p>  <br> 
       <p style="color:white;font-family:courier;font-size:18px">ans</p><input  class="exam-btn" type="text" name="q8" >
<br><br><br><br>

  <p style="color:white;font-family:courier;font-size:18px">9.A sequence of digits beginning with zero is considered to be …………….number. </p>  <br> 
       <p style="color:white;font-family:courier;font-size:18px">ans</p><input  class="exam-btn" type="text" name="q9" >
<br><br><br><br>

  <p style="color:white;font-family:courier;font-size:18px">10.………………. escape sequence represents the given number in octal form. </p>  <br> 
       <p style="color:white;font-family:courier;font-size:18px">ans</p>
       <input  class="exam-btn" type="text" name="q10" >
<br><br><br><br>
  <input style="border:none;outline:none;background:cyan;height:50px;width:180px;color:white;font-family:courier;font-weight:bolder;font-size:18px;border-radius:60px;cursor:pointer" type="submit" value="Submit quiz">
  </div>
  <div style="font-size:18px;color:white;font-weight:bolder;">
   <?php
   $answer1; 
   $answer2; 
   $answer3; 
   $answer4; 
   $answer5;   
   $answer6; 
   $answer7; 
   $answer8; 
   $answer9; 
   $answer10;   


   $answer1 = isset($_POST['q1']) ; 
   $answer2 = isset($_POST['q2']) ;
   $answer3 = isset($_POST['q3']) ;
   $answer4 = isset($_POST['q4']) ;
   $answer5 = isset($_POST['q5']) ;
   $answer6 = isset($_POST['q6']) ; 

$answer7 = isset($_POST['q7']) ;
   $answer8 = isset($_POST['q8']) ;
   $answer9 = isset($_POST['q9']) ;
   $answer10 = isset($_POST['q10']) ;

   $totalcorrect;
   $totalcorrect = 0;

   if($answer1 == "A") {$totalcorrect++;}
   if($answer2 == "B") {$totalcorrect++;}
   if($answer3 == "A") {$totalcorrect++;}
   if($answer4 == "D") {$totalcorrect++;}
   if($answer5 == "B") {$totalcorrect++;}
   if($answer6 == "token") {$totalcorrect++;}
   if($answer7 == "sensitive") {$totalcorrect++;}
   if($answer8 == "identifier") {$totalcorrect++;}
   if($answer9 == "octal") {$totalcorrect++;}
   if($answer10 == "\0n") {$totalcorrect++;}


   ?>

    </div>
</form>
</body>
</html>

 php code:
 <?php
 //include auth.php file on all secure pages

 ?>
 <html>
 <head>
 <title>online examination system</title>
 <link rel="stylesheet" type="text/css" href="style.css">
 <link rel="stylesheet" type="text/css" href="style2.css">    
 <meta charset="UTF-8">
 <script language="javascript" type="text/javascript"   src="libraries/p5.js"></script>
 <script language="javascript" src="libraries/p5.dom.js"></script>
 <script language="javascript" src="libraries/p5.sound.js"></script>
 <script language="javascript" type="text/javascript" src="sketch.js">  </script>
 </head>
 <body>
 <div class=header>
 <img class="img-style" src="Online-Examination-System-Banner.jpeg" width="1166px" height="250px">
 </div>
 <div class="top">
 <ul id="navigation">
  <li><a href="index.php">HOME</a></li>    
 <li><a href="aboutus.php">ABOUT US</a></li>


<li><a href="examination.php">EXAMINATION</a></li>
<li><a href="login.php">LOGIN</a></li>
<li><a href="logout.php">LOGOUT</a></li>

</ul> 
</div>
<p style="font-size:40;color:white;font-weight:bolder;">Welcome  !</p>

<br><br><br><br><center>
<div style="font-size:34px;color:cyan;font-weight:bolder;background:rgba(0,0,0,0.4)">
   <?php


   $answer1 = isset($_POST['q1']) ; 
   $answer2 = isset($_POST['q2']) ;
   $answer3 = isset($_POST['q3']) ;
   $answer4 = isset($_POST['q4']) ;
   $answer5 = isset($_POST['q5']) ;
   $answer6 = isset($_POST['q6']) ; 
   $answer7 = isset($_POST['q7']) ;
   $answer8 = isset($_POST['q8']) ;
   $answer9 = isset($_POST['q9']) ;
   $answer10 = isset($_POST['q10']) ;


   $totalcorrect = 0;

   if($answer1 == "A") {$totalcorrect++;}
   if($answer2 == "B") {$totalcorrect++;}
   if($answer3 == "A") {$totalcorrect++;}
   if($answer4 == "D") {$totalcorrect++;}
   if($answer5 == "B") {$totalcorrect++;}
   if($answer6 == "token") {$totalcorrect++;}
   if($answer7 == "sensitive") {$totalcorrect++;}
   if($answer8 == "identifier") {$totalcorrect++;}
   if($answer9 == "octal") {$totalcorrect++;}
   if($answer10 == "\0n") {$totalcorrect++;}

   echo "TOTAL CORRECT ANSWERS ARE &nbsp;".$totalcorrect."&nbsp; OUT OF  10 QUESTIONS";
   ?>
   <br><br><br>
  <p style="font-size:28;color:white;font-weight:bolder;font-family:courier">Fill up the following  data</p>


 <form action="thanks.php" method="post">
    <center><br><br>

    <label style="color:white;font-weight:bolder;font-family:courier">How would you like to rate the test</label><br><br>
    <table cellspacing="50">
        <tr>
            <td><img src="star.png" width="80px" height="80px"><br>& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="rate"></td>
            <td><img src="star.png" width="80px" height="80px"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="rate"></td>
            <td><img src="star.png" width="80px" height="80px"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="rate"></td>
            <td><img src="star.png" width="80px" height="80px"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="rate"></td>
            <td><img src="star.png" width="80px" height="80px"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="rate"></td>
        </tr>
        </table><br><br>
         <label style="color:white;font-weight:bolder;font-family:courier">Your experience during the test:</label><br>
    <textarea cols="50" rows="10">
        </textarea><br><br>
    <input style="border:none;outline:none;background:cyan;height:50px;width:180px;color:white;font-family:courier;font-weight:bolder;font-size:18px;border-radius:60px;cursor:pointer" type="submit" name="sub-feedback" value="SUBMIT FEEDBACK" ><br><br><br>
    </center></form>
    <br><br><br>
        </div>
</center>
</body>
</html>
nilesh
  • 1
  • 3
  • Adding imperatives like "urgent" and "hurry" [should not be added to questions](//meta.stackoverflow.com/q/326569/) as they will reduce the likelihood that you will receive answers to your question as it is just noise at best, and considered rude at worst. – John Conde May 07 '18 at 15:19
  • 3
    **Too much code**. You need to do a better job of troubleshooting this yourself. We are *not* debuggers. You need **isolate the problem** and debug from there. If you're stuck provide a **clear explanation of what isn't working** with a [**Minimal, Complete, and Verifiable example**](//stackoverflow.com/help/mcve). I suggest reading **[ask]** a good question and **[the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)**. Also, be sure to take the **[tour]** and read **[this](//meta.stackoverflow.com/q/347937/)**. – John Conde May 07 '18 at 15:19
  • tl;dr, chop out the unneccesary stuff – delboy1978uk May 07 '18 at 15:21
  • school project; I see. What have you done to fix this yourself? – Funk Forty Niner May 07 '18 at 15:22
  • @JohnConde i am sorry for that but i literally dont have aclue about the stuff much so sorry for this and i am in urgent need of a solution – nilesh May 07 '18 at 15:23
  • `$answerX = isset($_POST['X']) ;` will *always* be considered as being "set". – Funk Forty Niner May 07 '18 at 15:24
  • @FunkFortyNiner i tried to initialize the $answer variables separately with zero values but even that didnt work – nilesh May 07 '18 at 15:24
  • @FunkFortyNiner so shaal i remove the isset – nilesh May 07 '18 at 15:25
  • use conditionals, or ternaries; see the answer below – Funk Forty Niner May 07 '18 at 15:36
  • Welcome to StackOverflow. Please do read the posts suggested by John Conde and you'll find you'll get very good help. But it looks like you're in need of tutorial help, which you should turn to your instructor or classmates for. You definitely need to "have a clue about the stuff" you post here. You're lucky you got an answer, expecting the readers to figure out in all that code just what was wrong. – Jeff Learman May 07 '18 at 16:13

1 Answers1

1
$answer1 = isset($_POST['q1']) ; 

Isset checks if a variable is set (null). Returns true or false. So $answer1 is not equal to $_POST['q1'] but to true.

If you wanted to check if it was set and give it a value you would have to check if it is set THEN assign the value, i am just going to assign false if it isn't set :

$answer1 = (isset($_POST['q1'])) ? $_POST['q1'] : false;

Then, you check :

if($answer1 == "A") // if TRUE == "A"

While $answer = True, PHP also type juggles "A" to true. You need to read about PHP type juggling : http://php.net/manual/en/language.types.type-juggling.php

Once you corrected $answer1 = isset([...]) like mentioned above, you should strictly check the answer against your string, just to make sure :

if($answer1 === "A"){}

Notice the 3 "="s signs.

Also on your 10th question validation, you will need to either escape your backslash OR use single quotes arouns your answer :

if($answer10 === "\\0n") {$totalcorrect++;}

OR

if($answer10 === '\0n') {$totalcorrect++;}

Good read about double vs single quotes in php : What is the difference between single-quoted and double-quoted strings in PHP?

Lou
  • 866
  • 5
  • 14