0

I'm developing an online quiz that would let the user answer questions by clicking on a few radio buttons. I want the program to use the questions that are in the database. I've already created the connection through PHP but it's not properly passing through to the javascript code.


<body>

  <?php
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'psych';

$connect = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);

    $res = mysqli_query($connect, "SELECT questions FROM questions") or die( mysqli_error($connect));
;
    $quests = mysqli_fetch_array($res);  
    // echo "Quest0=$quests[0]";
      $scores=array();
      $i = "0";

        while($row = mysqli_fetch_array($res))
    {
       $arr[$i] = $row['questions'];
       $i++;

     }

  ?>
<div id="quiz"></div>
<br>
<button id="submit">Calculate</button>
<h4 id="message"></h4>


<br><br>

<div id="results"></div>



<script type="text/javascript">

const submitButton = document.getElementById('submit');

const array = <?php echo json_encode($arr); ?>; 



// Display the array elements 
for(var i = 0; i < array.length; i++){ 
    const myQuestions=[ {(array[i])}]; 
    //document.write(myQuestions[i]);
   //console.log(myQuestions[j]);
   displayArrayObjects(myQuestions);

} 

function displayArrayObjects(arrayObjects) {
        var len = arrayObjects.length, text = "";

        for (var i = 0; i < len; i++) {
            var myObject = arrayObjects[i];

            for (var x in myObject) {
                text += ( myObject[x] );
            }
            text += "<br/>";
        }

        document.getElementById("message").innerHTML = text;
    }

//const Myanswers = [a: "answer A", b: "answer B" ];

     const answers = [{ answers: {
          a: "answer A",
          b: "answer B"
        },
          answerA: "a",
          answerB: "b"
        },];



Dharman
  • 30,962
  • 25
  • 85
  • 135
shade27
  • 65
  • 10
  • Does ```$arr``` fetch data? – Yasin Feb 29 '20 at 05:20
  • Yes, it does. I think. When I uncomment //document.write(myQuestions); it does show the data from the databse – shade27 Feb 29 '20 at 05:38
  • You are missing a script ending tag. can you confirm? – Yasin Feb 29 '20 at 05:57
  • Yup, it's there. The data is only represented when i use a document.write(); Nothing else works – shade27 Feb 29 '20 at 06:08
  • One would normally select from the database more information than simply the text of of the question – Strawberry Feb 29 '20 at 07:18
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Feb 29 '20 at 16:36

1 Answers1

-1

You have some syntax errors.


<body>

<?php
    $arr = array('q1','q2','q3');
?>
<div id="quiz"></div>
<br>
<button id="submit">Calculate</button>
<h4 id="message"></h4>


<br><br>

<div id="results"></div>



<script type="text/javascript">

const submitButton = document.getElementById('submit');

const array = <?php echo json_encode($arr); ?>; 



  let myQuestions = [];
// Display the array elements 
for(var i = 0; i < array.length; i++){ 
   myQuestions.push(array[i]); 
  //document.write(myQuestions[i]);
}
 console.log(myQuestions);
 displayArrayObjects(myQuestions);

function displayArrayObjects(arrayObjects) {
      var len = arrayObjects.length, text = "";

      for (var i = 0; i < len; i++) {
          var myObject = arrayObjects[i];

          for (var x in myObject) {
              text += ( myObject[x] );
          }
          text += "<br/>";
      }

      document.getElementById("message").innerHTML = text;
  }

//const Myanswers = [a: "answer A", b: "answer B" ];

   const answers = [{ answers: {
        a: "answer A",
        b: "answer B"
      },
        answerA: "a",
        answerB: "b"
      },];

</script>
Yasin
  • 124
  • 2
  • 11
  • Thank you so much mate! The questions are working. but the issue is it always skips the first entry in database – shade27 Mar 01 '20 at 14:51
  • Also, could you please help me display the radio buttons under the questions? They're supposed to be the answers and I'm supposed to calculate a value for them according to the answers they give. Sounds like something you could help me with? You're a lifesaver! – shade27 Mar 01 '20 at 14:53
  • Is it solved yet? Also don't mind, your response time needs to much quicker. – Yasin Mar 04 '20 at 09:05