473,670 Members | 2,397 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 3639
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
9245
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 VALUE=" " NAME=cat, and a gif next to it. User will click on the icon to get a new window. In the new window, the user selects a value from the pull down menu (created by servlet from back end) and clicks OK. The value is passed back to the
5
7077
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 do that? I tried to declare this variable as Public in my form, but it does not work - when I reference to this variable in my report, it does not pass the value.
1
3138
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 advance... Ben
1
2415
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 ----------------------------------------------------------------------- Posted via http://www.codecomments.co -----------------------------------------------------------------------
0
2386
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. so far, I can only finished the first part which is to delete the row from database, Please see below code where I want to get variable in order to delete the directory. Thanks in advance <%@ Page Language="VB" Debug="true" %> <script...
1
4057
by: vijaynarang | last post by:
strCmdLine = " " + " -o restore -url " + sSitePath + "/sites/" + sToplevelSiteName + " -filename " + sFileName+ " -overwrite "; process1 = System.Diagnostics.Process.Start(Application.StartupPath + @"\" + "STSADM.EXE", strCmdLine); process1.WaitForExit(); process1.Close(); I want double qoute around sfilename Please tell me how to pass variable containing ' " " (double qoute)
6
2508
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 "<div><small>Test</small></div>" My problem is that I don't know how to display this property on the page. I want the value to be displayed as any other html code... I don't want the
4
13261
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 = formdiv.innerHTML +'<td align=center> java script variable x</td>' i need to write the value of this X between the tags and i tried thousands of solutions but none of them worked :(
12
6891
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 href='home.php?page=$lastpage'>NEXT</a>).if i click on next in the tab1 on home page(home.php) it will pass the variable and bring me to the home page and tab1 if i click on the next in tab2 it will also bring me to home page and take me to tab1 back.i want it to bring...
2
2885
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 MainForm. 2.) Button B : User Click Button B,hide Mainform then go to form2. Click a button & some mathematical operations will be done using all parameters that have been entered in form1. My method of passing variable is:
0
8386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8903
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8592
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8661
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5684
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4211
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2800
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2042
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1794
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.