How to pass a variable from JS script to HTML

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sharpda
    New Member
    • Feb 2013
    • 2

    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.

    Code:
    <script type="text/jscript">
        function quizResult(form, questionArray) {
            totalQuestions = form.totalQuestions.value;
            totalCorrect = 0;
            checkedValue = new Array();
            quiz.style.visibility = "hidden";
            var n = prompt("Enter UserID Please", "Type your Name here");
    
    
            for (counter1 = 0; counter1 < totalQuestions; counter1++) {
                currentQuestion = eval("form.Q" + String(counter1));
                currentAnswer = eval("form.A" + String(counter1));
                checkedValue[counter1] = "NA";
                for (counter2 = 0; counter2 < 4; counter2++) {
                    if (currentQuestion[counter2].checked) {
                        checkedValue[counter1] = currentQuestion[counter2].value;
                    }
                }
                formComplete = true;
                if (checkedValue[counter1] == currentAnswer.value) {
                    totalCorrect++;
                }
                else if (checkedValue[counter1] == "NA") {
                    formComplete = false;
                    break;
                }
            }
            if (formComplete) {
    
                for (counter3 = 0; counter3 < totalQuestions; counter3++) {
                    document.write("<p>");
                    document.write("<b>Question: " + questionArray[counter3][0] + "</b><br><br>");
                    if (questionArray[counter3][5] == checkedValue[counter3]) {
                        document.write("<b>Your Answer: </b>" + questionArray[counter3][checkedValue[counter3]] + "<b><span style='color: #25E01B'> -- Correct</span></b><br>");
                    }
                    else {
                        document.write("<b>Your Answer: </b>" + questionArray[counter3][checkedValue[counter3]] + "<b><span style='color: #FF0000'>-- Incorrect</span></b><br>");
                        document.write("<b>Correct Answer: </b>" + questionArray[counter3][questionArray[counter3][5]] + "<br>");
                    }
                    document.write("</p>");
                }
                totalIncorrect = totalQuestions - totalCorrect;
                percentage = Math.floor((totalCorrect / totalQuestions * 100));
                document.write("<p><br>  <b> === QUIZ RESULTS FOR === <b>" + n + "<br> <br>");
                document.write("<b>Total Questions: </b>" + totalQuestions + "<br>");
                document.write("<b>Correct Answers:</b> " + totalCorrect + "<br>");
                document.write("<b>Incorrect Answers: </b> " + totalIncorrect + "<br>");
                document.write("Result: " + percentage + "%</p>");
    
                document.write(percentage);
    
                if (percentage >= 75) {
                    document.write("  <h3> Congratulation You Pass the Quiz </h3>");
     
                }
                else {
                    document.write('<H2><b><span style="color: #FF0000"> Sorry, You did not Pass the Quiz. Try Again.    </H2><b></span>');
    
                }
            }
            else {
                alert("You did not complete the quiz!");
                quiz.style.visibility = "visible";
            }
    
        }
    
    
    
        
        
        function Redirect() {
            top.location.replace('Timeover.html');
        }
        function RedirectWithDelay() {
            window.setTimeout("Redirect();", 60000);
        } 
    </script>
    Last edited by Rabbit; Feb 26 '13, 10:10 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    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.

    Comment

    • sharpda
      New Member
      • Feb 2013
      • 2

      #3
      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.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        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.

        Comment

        • Sherin
          New Member
          • Jan 2020
          • 77

          #5
          Try This Code

          Code:
          <div id="bikeDiv">
          </div>
          <button id="addbikeBtn">Add Bike</button>
          <script>
          var current = 0;
          var cars = new Array(4);
          cars[0] = "TVS";
          cars[1] = "Yamaha";
          cars[2] = "Royal Enfield";
          cars[3] = "Bajaj Pulsar";
          document.getElementById("addbikeBtn").onclick = function() {
                 if (!(current > bikes.length - 1))
                 {
                      document.getElementById("bikeDiv").innerHTML += bikes[current] + "<br />";
                      current++;
                 }
          }
          </script>

          Comment

          Working...