Click here to Skip to main content
15,887,331 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello.I have to make a web application with Visual Studio .NET ASP.I make some quiz with javascript.Now i want to take the RESULT value of the quiz and store it to my Database..
How is it possible?
Below there is my script.
From that script i want to take the final score value and save it to my database.

What I have tried:

JavaScript
var score = 0; //Set score to 0
var total = 5; //Total number of questions
var point = 2; //Points per correct answer
var highest = total * point;



//Initializer
function init(){
	//set correct answers
	sessionStorage.setItem('a1','c');
	sessionStorage.setItem('a2','c');
	sessionStorage.setItem('a3','a');
	sessionStorage.setItem('a4','c');
	sessionStorage.setItem('a5','a');
}

$(document).ready(function(){
	//Hide all questions
	$('.questionForm').hide();
	
	//Show first question
	$('#q1').show();
	
	$('.questionForm #submit').click(function(){
		//Get data attributes
		current = $(this).parents('form:first').data('question');
		next = $(this).parents('form:first').data('question')+1;
		//Hide all questions
		$('.questionForm').hide();
		//Show next question
		$('#q'+next+'').fadeIn(300);
		process(''+current+'');
		return false;
	});
});

//Process the answers
function process(n){
	//Get input value
	var submitted = $('input[name=q'+n+']:checked').val();
	if(submitted == sessionStorage.getItem('a'+n+'')){
			score = score + point;
	}
		
	if(n == total){	
		$('#results').html('<h3>Your final score is: '+score+' out of '+highest+'</h3><a href="/KB/answers/EnglishTest1.aspx">Take Quiz Again</a>');
		if(score == highest){
			$('#results').append('<p>You are an Enghlish master!');
		} else if(score == highest - point || score == highest - point - point){
			$('#results').append('<p>Good Job!');
		}
		 else  {
            $('#results').append('<p>You have not study enough.Study and come back later !');
        }
	}
	return false;
}




//Add event listener
window.addEventListener('load',init,false);
Posted
Updated 25-Jun-18 2:18am

Based on what posted here, not clear which .NET version/technology the application is using, but look like it using JavaScript + jQuery on the client side. Anyway, the idea should be the same, you can create a Web API, on client side, use the jQuery to post to the API method with the results as data. Here are few examples on how to call Web API through jQuery.

https://www.c-sharpcorner.com/article/call-asp-net-webapi-using-jquery/[^]
Calling Web APIs from jQuery and JavaScript[^]
Calling an ASP.NET Web API from jQuery - Carl's Blog[^]
https://www.c-sharpcorner.com/UploadFile/dacca2/web-api-with-ajax-understand-post-request-in-web-api/
https://www.aspsnippets.com/Articles/Call-Consume-Web-API-using-jQuery-AJAX-in-ASPNet-MVC.aspx
 
Share this answer
 
Comments
Member 13885086 25-Jun-18 6:27am    
Im using Visual Studio 2017
I find it..To take a value from Javascript to your local Database at Visual Studio with ASP you have to add at .aspx
ASP.NET
<asp:HiddenField runat="server" ID="hfProduct" ClientIDMode="Static" />

Then add at Javascript
JavaScript
<pre>$("#hfProduct").val(score);



hfProduct is your HiddenField ID and score at javascript is the value you want to take.

Then at .aspx.cs add this
C#
string score;
        score = hfProduct.Value.ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900