468,769 Members | 2,255 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,769 developers. It's quick & easy.

How to pass a variable from JS script to HTML

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
4 2860
Rabbit
12,515 Expert Mod 8TB
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
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
12,515 Expert Mod 8TB
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
Sherin
77 64KB
Try This Code

Expand|Select|Wrap|Line Numbers
  1. <div id="bikeDiv">
  2. </div>
  3. <button id="addbikeBtn">Add Bike</button>
  4. <script>
  5. var current = 0;
  6. var cars = new Array(4);
  7. cars[0] = "TVS";
  8. cars[1] = "Yamaha";
  9. cars[2] = "Royal Enfield";
  10. cars[3] = "Bajaj Pulsar";
  11. document.getElementById("addbikeBtn").onclick = function() {
  12.        if (!(current > bikes.length - 1))
  13.        {
  14.             document.getElementById("bikeDiv").innerHTML += bikes[current] + "<br />";
  15.             current++;
  16.        }
  17. }
  18. </script>
Mar 2 '21 #5

Post your reply

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

Similar topics

1 post views Thread by dollar | last post: by
reply views Thread by weiwei | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.