By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,044 Members | 1,330 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,044 IT Pros & Developers. It's quick & easy.

How to pass a variable from JS script to HTML

P: 2
I'm trying to create an online quiz that uses JS script. What I'd like to do is take the varibles "Percentage" and "n" from the JS script function and insert them into MySql. Code below.

Expand|Select|Wrap|Line Numbers
  1. <script type="text/jscript">
  2.     function quizResult(form, questionArray) {
  3.         totalQuestions = form.totalQuestions.value;
  4.         totalCorrect = 0;
  5.         checkedValue = new Array();
  6.         quiz.style.visibility = "hidden";
  7.         var n = prompt("Enter UserID Please", "Type your Name here");
  8.  
  9.  
  10.         for (counter1 = 0; counter1 < totalQuestions; counter1++) {
  11.             currentQuestion = eval("form.Q" + String(counter1));
  12.             currentAnswer = eval("form.A" + String(counter1));
  13.             checkedValue[counter1] = "NA";
  14.             for (counter2 = 0; counter2 < 4; counter2++) {
  15.                 if (currentQuestion[counter2].checked) {
  16.                     checkedValue[counter1] = currentQuestion[counter2].value;
  17.                 }
  18.             }
  19.             formComplete = true;
  20.             if (checkedValue[counter1] == currentAnswer.value) {
  21.                 totalCorrect++;
  22.             }
  23.             else if (checkedValue[counter1] == "NA") {
  24.                 formComplete = false;
  25.                 break;
  26.             }
  27.         }
  28.         if (formComplete) {
  29.  
  30.             for (counter3 = 0; counter3 < totalQuestions; counter3++) {
  31.                 document.write("<p>");
  32.                 document.write("<b>Question: " + questionArray[counter3][0] + "</b><br><br>");
  33.                 if (questionArray[counter3][5] == checkedValue[counter3]) {
  34.                     document.write("<b>Your Answer: </b>" + questionArray[counter3][checkedValue[counter3]] + "<b><span style='color: #25E01B'> -- Correct</span></b><br>");
  35.                 }
  36.                 else {
  37.                     document.write("<b>Your Answer: </b>" + questionArray[counter3][checkedValue[counter3]] + "<b><span style='color: #FF0000'>-- Incorrect</span></b><br>");
  38.                     document.write("<b>Correct Answer: </b>" + questionArray[counter3][questionArray[counter3][5]] + "<br>");
  39.                 }
  40.                 document.write("</p>");
  41.             }
  42.             totalIncorrect = totalQuestions - totalCorrect;
  43.             percentage = Math.floor((totalCorrect / totalQuestions * 100));
  44.             document.write("<p><br>  <b> === QUIZ RESULTS FOR === <b>" + n + "<br> <br>");
  45.             document.write("<b>Total Questions: </b>" + totalQuestions + "<br>");
  46.             document.write("<b>Correct Answers:</b> " + totalCorrect + "<br>");
  47.             document.write("<b>Incorrect Answers: </b> " + totalIncorrect + "<br>");
  48.             document.write("Result: " + percentage + "%</p>");
  49.  
  50.             document.write(percentage);
  51.  
  52.             if (percentage >= 75) {
  53.                 document.write("  <h3> Congratulation You Pass the Quiz </h3>");
  54.  
  55.             }
  56.             else {
  57.                 document.write('<H2><b><span style="color: #FF0000"> Sorry, You did not Pass the Quiz. Try Again.    </H2><b></span>');
  58.  
  59.             }
  60.         }
  61.         else {
  62.             alert("You did not complete the quiz!");
  63.             quiz.style.visibility = "visible";
  64.         }
  65.  
  66.     }
  67.  
  68.  
  69.  
  70.  
  71.  
  72.     function Redirect() {
  73.         top.location.replace('Timeover.html');
  74.     }
  75.     function RedirectWithDelay() {
  76.         window.setTimeout("Redirect();", 60000);
  77.     } 
  78. </script>
Feb 26 '13 #1
Share this Question
Share on Google+
3 Replies


Rabbit
Expert Mod 10K+
P: 12,430
Unless the client is on the same LAN as the server, you won't be able to use Javascript to insert data into the database. You will have to pass the values to a client side script which will then do the insert.
Feb 26 '13 #2

P: 2
Thanks for the input. This quiz will be on a LAN "Intranet". Could you please provide some sample code that would insert the values in the MYSQL database. I am having a hard time with this for some reason. Thx.
Feb 26 '13 #3

Rabbit
Expert Mod 10K+
P: 12,430
Even on a LAN, you really should submit the data to a server side script. You don't want to give the client that kind of access to the backend, it's a huge security risk.
Feb 27 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.