Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Passing Data from Javascript to ASP using hidden form post

Posted on 2011-09-21
Medium Priority
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

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

Open in new window

Question by:OGSan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2

Expert Comment

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

Author Comment

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.
LVL 15

Expert Comment

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

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";   
	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;

Open in new window

LVL 15

Accepted Solution

Eyal earned 2000 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)

Author Comment

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.

Author Comment

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.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

688 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