0

I am building a quiz, I have constructed the database (I will input an example of what my tables look like below. I am having an issue getting the user's answers to actually be placed in the database? Right now, I can load the quiz on my browser, answer the questions, but I cannot actually figure out my score on the quiz.

Database

questions

ID      question
1       Question1?
2       Question2?

etc

choices

ID       questID     answer
1        1           Ansa
2        1           Ansb
3        1           Ansc
1        2           Ansa
2        2           Ansb
3        2           Ansc
4        2           Ansd

etc

userans

userID     questID     answerID

right now userans table is empty because that is where the user's answers are supposed to be put

My quiz is pretty simple, but I just need help getting the database & quiz to "talk" to each other. Any help is appreciated!

Quiz code

<!DOCTYPE html>
<html>
<head>
<title>Quiz</title>
<script language="Javascript">

function process()
{
var A = 0;
var B = 0;
var C = 0;
var D = 0;

var f = document.f;
var i = 0;

for (i = 0; i < f.q1.length; i++) if (f.q1[i].checked) value = f.q1[i].value;
    if (value == "13") { } 
    if (value == "11") { C++; A++; B++; }
    if (value == "5") { C++; D++; A++; B++; }

for (i = 0; i < f.q2.length; i++) if (f.q2[i].checked) value = f.q2[i].value;
    if (value == "5") { C++; D++; A++; B++; }
    if (value == "12") { D++; A++; B++; }
    if (value == "11") { C++; A++; B++; }
    if (value == "10") { C++; D++; A++; }

/*Copy the above for loop for all subsequent questions, if needed*/

var out = "A"; 
i = "a"; 

if (C > i) { out ="C"; i = "c"; } 
if (B > i) { out ="B"; i = "b"; }
if (D > i) { out ="D"; i = "d"; } 
window.alert ("Result: " + i + " !!");
}

</script>
</head>

<body>
<?php include ('Menu2.html'); ?>
<p>Answer the questions below</p> <br>

<form name="f">

<b>Question1<br></b>
    <input type="radio" name="q1" value="1">Ansa<br>
    <input type="radio" name="q1" value="2">Ansb<br>
    <input type="radio" name="q1" value="3">Ansc<br><br>

<b>Question2<br></b>
    <input type="radio" name="q2" value="1">Ansa<br>        
    <input type="radio" name="q2" value="2">Ansb<br>
    <input type="radio" name="q2" value="3">Ansc<br>
    <input type="radio" name="q2" value="4">Ansd<br><br>

/*Input more questions, if needed, here*/

Thanks for taking the quiz! <br>
<input type="button" value="Score!" onclick="process();"><br><br>
</form>
</body>
</html>

1 Answers1

1

Javascript is a client side language - in order to connect to a database, you'll need to use server side software such as PHP. Your HTML form can submit content via GET or POST, or you can use Ajax to push/pull information, but cannot be done via Javascript.

Check out some examples: Can JavaScript connect with MySQL? How can I use jQuery to run MySQL queries?

Community
  • 1
  • 1
AutomationNation
  • 527
  • 1
  • 4
  • 10