0

this is my code and i am getting an error of undefined index q1,q2,q3,q4,q5 please help,i tried a few different ways to resolve the problem was not able to find any solution anywhere,i am new to php programming and currently i am using wampserver. i have attached my html file as well.the name of my input radio buttons is q1,q2,q3,q4,q5 respectively for each question

<?php
$answer1 = $_POST['q1'];
$answer2 = $_POST['q2'];
$answer3 = $_POST['q3'];
$answer4 = $_POST['q4'];
$answer5 = $_POST['q5'];
$totalcorrect = 0;
if($answer1 == "A") {$totalcorrect++;}
if($answer2 == "B") {$totalcorrect++;}
if($answer3 == "A") {$totalcorrect++;}
if($answer4 == "D") {$totalcorrect++;}
if($answer5 == "B") {$totalcorrect++;}
echo "total correct answers are".$total;?>
html code:
<div style="margin-top:10px;margin-right:85px;margin-left:100px;margin-   bottom:20px;background:rgba(0,0,0,0.5)">
//question 1   
<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" checked>
           <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>
   //question2
   <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" checked>
           <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" checked>
           <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" checked>
           <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" checked>
           <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>
</div>
nilesh
  • 1
  • 3

3 Answers3

2

You have "undefined variable" $_POST['q1'] if the field q1 is not posted (and so on for q2,...). You should use isset() to avoid warnings.

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

As of PHP 7.0, you could use the null coalescing operator using the ?? operator :

$answer1 = $_POST['q1'] ?? '';
$answer2 = $_POST['q2'] ?? '';
$answer3 = $_POST['q3'] ?? '';
$answer4 = $_POST['q4'] ?? '';
$answer5 = $_POST['q5'] ?? '';

Finally (as @C2486 pointed out), the last variable should be $totalcorrect instead of $total.

echo "total correct answers are" . $totalcorrect;
Syscall
  • 19,327
  • 10
  • 37
  • 52
0

In this case, may be your $_POST is undefined. So to check that , kindly run the following statement ,

print_r($_POST);

Let me know what you get, also kindly check and show the code by which you are sending the POST request.

bansal
  • 99
  • 8
-1

Are you sure you are sending the data through a post request?

Try this:

<?php
$answer1 = $_REQUEST['q1'];
$answer2 = $_REQUEST['q2'];
$answer3 = $_REQUEST['q3'];
$answer4 = $_REQUEST['q4'];
$answer5 = $_REQUEST['q5'];
$totalcorrect = 0;
if($answer1 == "A") {$totalcorrect++;}
if($answer2 == "B") {$totalcorrect++;}
if($answer3 == "A") {$totalcorrect++;}
if($answer4 == "D") {$totalcorrect++;}
if($answer5 == "B") {$totalcorrect++;}
echo "total correct answers are".$total;?>

And then send a post request or you can send a get request like this:

http://yourip:port/file.php?q1=A&q2=A&q3=A&q4=A&q5=A

Marti157
  • 81
  • 1
  • 7
  • A good practice is not to use `$_REQUEST`. It is better to think what exactly the app/website is going to communicate, and engineer it correctly https://stackoverflow.com/a/1924958/5459467 – Cukic0d May 05 '18 at 11:07