Solved

Passing Data from Javascript to ASP using hidden form post

Posted on 2011-09-21
7
420 Views
Last Modified: 2012-05-12
Hi, Experts -
We test new job applicants on shared PCs in a testing room.  We run testing software that uses a Javascript module to capture score info.  We have modified this Javascript module so that it passes the score info to an ASP program (using method=post and hidden form fields).  On the receiving end, the ASP program uses a Request.Form to retrieve the data passed to it - and then loads it into a database table.  The data being passed should consist of five elements for each of the 25 questions on a quiz - a total of 125 items.
Most of the time, the program works without incident.  But every once in a while, the ASP program will abort with a "subscript out of range" error because the code assumes that there will be five elements for each question - and sometimes the Javascript module does not pass all 25 questions over to the ASP program (confirmed this with onscreen displays).
So, I have a two-part question:
Part 1.  Why would the Javascript pgm not pass all of the data over to the ASP program - on occasion - and what can we do to minimize this occurrence (like restart the PC to flush the IE cache)?
Part 2.  How can I modify the ASP code to account for the possibility that not all the data was passed (see my snippet from the ASP program below) - and continue to update the database with whatever was passed?
for i = 0 to (Ubound(LineDetails) - 1) Step 5
			vstrObjectiveID 	= LineDetails(i)
			response.write("D E B U G   1) vstrObjectiveID = " & vstrObjectiveID & "<br>")
			vstrInteractionType = LineDetails(i+1)
			vstrResponse 		= LineDetails(i+2)
			vstrResult 			= LineDetails(i+3)
			vstrLatency 		= LineDetails(i+4)

			' Create a new record in recordset
			rsDtl.AddNew

			if vstrLastName <> "" 		then rsDtl("dtl_LastName")= vstrLastName 			else rsDtl("dtl_LastName")=" "

Open in new window

0
Comment
Question by:OGSan
  • 4
  • 2
7 Comments
 
LVL 5

Expert Comment

by:Eduardo Goicovich
ID: 36578118
I can see all the code. Can you repost?
0
 
LVL 1

Author Comment

by:OGSan
ID: 36578139
Hi, belfegor - I don't mind reposting all the code, except that I don't think it adds any insight to the problem.  If you see the snippet of code above, the problem is that the for/next loop steps by 5 in anticipation of the five elements per question it ASSUMES is being passed.  When there aren't five elements passed, the code bombs out where it references an "i+X" value because...there's nothing there.  I need a way to improve those hardcoded subscript values that are assuming five elements will be passed - but I'm not sure how to do that.
Hope that makes sense, and helps to clarify the Part 2 issue.
0
 
LVL 15

Expert Comment

by:Eyal
ID: 36578254
1) can you post the JavaScript that gathers the data and sends it to the server
2) on what row it is failing? start from 5 and take the values from array with -

for i = 5 to (Ubound(LineDetails) - 1) Step 5
			vstrObjectiveID 	= LineDetails(i-4)
			response.write("D E B U G   1) vstrObjectiveID = " & vstrObjectiveID & "<br>")
			vstrInteractionType = LineDetails(i-3)
			vstrResponse 		= LineDetails(i-2)

...

Open in new window

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:OGSan
ID: 36578290
Below is the javascript function in the module that winds up passing the score info to the ASP program.  Sorry, it's called "EmailResults" but don't let it confuse you.
Re: what row it fails on?  It varies.  The last occurence of the problem had just 3 items out of 5 passed...and the last question was only the 16th (out of 25).  Yet I know that all 25 questions were answered because we can reload the page and it gets sent over...all 25...?!?  So odd.
function EmailResults(strAddress)
{
	if (!g_oQuizResults.strTitle)
	{
		g_oQuizResults.strTitle = "";
	}
	
	var g_strSubject = "Quiz Results: " + g_oQuizResults.strTitle;
	var strQuizResults = "";
	var strMainHeader = "\nQuiz Name, Status, Raw Score, Passing Score, Max Score, Min Score, Date Tkn, Time Tkn\n";
	var strMainData = "";
	var strLineData = "";
	
	var strQuizName = "";
	var strQuizStatus = "";
	var strQuizScore = "";
	var strQuizRaw = "";
	var strQuizPassing = "";
	var strQuizMax = "";
	var strQuizMin = "";
	var strQuizDate = "";
	var strQuizTime = "";

	// Quiz Name
	strQuizName += g_oQuizResults.strTitle;
	
	// Status
	strQuizStatus += g_oQuizResults.strResult;
	
	// Score
	strQuizScore += g_oQuizResults.strScore;
	
	// Raw Score
	strQuizRaw += g_oQuizResults.strPtScore;
	
	// Passing Score
	strQuizPassing += Math.round((g_oQuizResults.strPassingScore/100) * g_oQuizResults.strPtMax);
	
	// Max Score
	strQuizMax += g_oQuizResults.strPtMax;
	
	// Min Score
	strQuizMin += 0;
	
	// Date
	strQuizDate += GetDate(g_oQuizResults.dtmFinished);
	
	// Time
	strQuizTime += GetTime(g_oQuizResults.dtmFinished);
	
// **** 08-09-10 JTF Inactivated following code - do not need this level of detail at this time. 
// **** 09-20-11 JTF ACTIVATED parts of the following quiz line details for saving in new tbl_quiz_detail.
//                   Changed delimiter character from comma to pipe in order to facilitate Split performed
//                   by QM_Post_Results.asp (on the server).
	for (var i = 0; i < g_arrResults.length; i++)
	{
		
		// Objective Id
		strLineData += g_arrResults[i].strObjectiveId + "|";

		// Interaction Type
		strLineData += g_arrResults[i].strType + "|";

		// Student Response
		var strResponse = g_arrResults[i].strStudentResponse;
		strResponse = ReplaceAll(strResponse, "'", "%27");
		strLineData += strResponse + "|";
		
		// Result
		strLineData += g_arrResults[i].strResult + "|";
		
		// Weight
//		strLineData += "1,";
		
		// Latency
		strLineData += g_arrResults[i].strLatency + "|";

// 09-20-11  JTF  Had to remove \n to get the ObjectiveID to write to the mdb		
//		strLineData += "\n";   
		strLineData;
	}
	
	strQuizLineDtl = strLineData;

// ***** 08-09-10 JTF Formatting for passing fields to ASP program, "QM_Post_Results.asp"
	var sHTML = "";
	sHTML += '<FORM id="formQuiz" method="POST" action="/hr/QuizMaker/QM_Post_Results.asp">';
	sHTML += '<INPUT TYPE="hidden" id="strQuizName" name="strQuizName" VALUE="' + strQuizName + '">';
	sHTML += '<INPUT TYPE="hidden" id="strQuizStatus" name="strQuizStatus" VALUE="' + strQuizStatus + '">';
	sHTML += '<INPUT TYPE="hidden" id="strQuizScore" name="strQuizScore" VALUE="' + strQuizScore + '">';
	sHTML += '<INPUT TYPE="hidden" id="strQuizRaw" name="strQuizRaw" VALUE="' + strQuizRaw + '">';
	sHTML += '<INPUT TYPE="hidden" id="strQuizPassing" name="strQuizPassing" VALUE="' + strQuizPassing + '">';
	sHTML += '<INPUT TYPE="hidden" id="strQuizMax" name="strQuizMax" VALUE="' + strQuizMax + '">';
	sHTML += '<INPUT TYPE="hidden" id="strQuizMin" name="strQuizMin" VALUE="' + strQuizMin + '">';
	sHTML += '<INPUT TYPE="hidden" id="strQuizDate" name="strQuizDate" VALUE="' + strQuizDate + '">';
	sHTML += '<INPUT TYPE="hidden" id="strQuizTime" name="strQuizTime" VALUE="' + strQuizTime + '">';
	// 09-19-11 JTF  Inserting additional detail for each response
	sHTML += '<INPUT TYPE="hidden" id="strQuizLineDtl" name="strQuizLineDtl" VALUE="' + strQuizLineDtl + '">';
	sHTML += '<br><input type="submit"><br>';
	sHTML += '</FORM>';

	document.getElementById("divEmail").innerHTML = sHTML;
	document.getElementById("formQuiz").submit();
}

Open in new window

0
 
LVL 15

Accepted Solution

by:
Eyal earned 500 total points
ID: 36578363
can you reproduce the error in Firefox? if so try to debug the javascript.

if some user will put " character what will happen? as I understand the code it will break (some of the text will not be included in the value of the hidden field)
0
 
LVL 1

Author Comment

by:OGSan
ID: 36582999
Thanks, Eyal - I think you've given me the key.  I bet that Javascript module needs to replace those values, otherwise...kablooiee!  I will research this and get back soonest.

BTW, I misspoke in my previous remark, "...we can reload the page it all gets sent over..."  WRONG!
We only get what the Javascript module has sent.
Sorry if that caused misdirection to anyone.
0
 
LVL 1

Author Comment

by:OGSan
ID: 36583211
Eyal - You da man!  That was the culprit: there were double-quotes being used in two of the 50 questions that form the pool of questions for this exam.  When the software happened to select one of those 2 questions - and it tried to save the response with the double-quote - boom!
Problem fixed.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp syntax 3 23
Accessing data in JSON response.d 4 29
Understanding Trigger in Jquery 10 27
getting id from database 5 24
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now