473,511 Members | 12,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pass a variable from JS script to HTML

2 New Member
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 3633
Rabbit
12,516 Recognized Expert Moderator MVP
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
sharpda
2 New Member
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,516 Recognized Expert Moderator MVP
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 New Member
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

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

Similar topics

2
9229
by: umashd | last post by:
Hi, I am doing a web based project for my graduation. I studied bit of java for backend processesing and javascript for the client. Here is the scenario. In the FORM, I use INPUT TYPE=text...
5
7068
by: GB | last post by:
Hello: In my MS Access project I have two objects - my form and my report. I need to pass variable value from Command_click procedure of my form to Report_open procedure of my report. How can I...
1
3129
by: Ben Kial | last post by:
I'd like to write a wrapper function "mysprintf(char *buffer, char *format, ....)" which calls sprintf(). My question is how can I pass variable argument in mysprintf() to sprintf(). Thanks in...
1
2410
by: dollar | last post by:
I want to ask what is the best way to pass variable between asp.net we pages, and user controls , any examples - dolla -----------------------------------------------------------------------...
0
2382
by: weiwei | last post by:
Hi; I am having trouble to get variable from grid view. here is my scenario. I want to delete a row in database from web page, in additon, I also want to delete that user's directory in c:drive. ...
1
4047
by: vijaynarang | last post by:
strCmdLine = " " + " -o restore -url " + sSitePath + "/sites/" + sToplevelSiteName + " -filename " + sFileName+ " -overwrite "; process1 =...
6
2494
by: Jeff | last post by:
Hey ASP.NET 2.0 I've added some extra properties to the Profile of users on my site. One of the new properties is of type "string" and it contain HTML data.... like for example...
4
13253
by: jessy | last post by:
i need to know if its possible to write the value of a Javascript variable inside HTML tag ..and here's the line : var x=document.getElementById('div1').value; formdiv.innerHTML =...
12
6822
by: simon2x1 | last post by:
i have a page which is home.php on that page i have a CSS tab which is tab1 and tab2 on that page in both tabs i have a link called next that pass variable to the next page (<a...
2
2872
by: peachdot | last post by:
hi, The MainForm will have 2 buttons: 1.) Button A : User click button A, hide Mainform then go to form1. User enter data in the textbox.Click finish button,form1 close then go back to...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.