[Last Call] Learn how to a build a cloud-first strategyRegister Now


Pass javascript variable to ASP variable

Posted on 2008-11-06
Medium Priority
Last Modified: 2008-11-13
My website shows a live stock count, and I want to create a popup window or have something display on the page when a customer tries to purchase a larger amount than we have in stock (just a warning that we may not be able to fill the order).  Below is the current add to cart buton and javascript that adds it to the cart.  The ASP variable in this page for stock count is sStock.  Any help would be appreciated!

<form action="javascript:preprocess('0','<%=iUnits%>');">        		    
<table class="mytable"><tr><td>
<input type="text"  name="qty" size="2" value="<%=iUnits%>" maxlength="8" tabindex=1>
<a href="javascript:preprocess('0','<%=iUnits%>');">
<img height=26 src="images/newcart.jpg"  ALIGN=baseline border="0" alt="ADD TO CART"></a>

Open in new window

Question by:ximbuex
  • 4
  • 3
LVL 21

Expert Comment

ID: 22895833
you can either place in hidden field or  you can pass through querystring...
LVL 21

Expert Comment

ID: 22895874
unless you want to go further and use ajax and webservices to pass variable which is overkill...

question:  are you trying to pass javascript value to asp on an event?  

so in the javascript:  you would:

document.getElementById("<%=hidField.ClientID%>").Value = variable...

<asp:Hiddenfield id="hidField" runat ="server" value=""/> and later asp can refer to it...
LVL 21

Expert Comment

ID: 22895898
if you're using classic asp, then same applies...except you wouldn't be using asp control...

<input type="hidden" value="">...in this case you would have to create a session since on postback, the value would revert back to original value.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 22896127
This is a little over my head because I know very little javascript and ASP, but I'm trying to read up on it so that I can understand what you're saying.  

To answer your question, I am trying to pass the javascript value thats enterted into the form to an ASP (or even javascript function that uses the ASP variable for available stock), then calculate to make sure that available stock (sStock ASP variable) is equal to or greater than the quantity that the customer is trying to purchase.  Ideally it would say "We have 5 in stock" and if the customer enters 6 and clicks 'add to cart', it pops up and says "We only have six in stock.  Click ok to continue with order and risk it being canceled or click cancel to enter a different quantity".  

LVL 21

Expert Comment

ID: 22896245

<input type="hidden" value="">       <------------this is a hidden field...hidden only because it can't be seen like a textbox field
                                                                       used to passed data between javascript and other languages....

the problem is:  what's in your preprocess script?  can you find that script?  you probably will need it...you probably will have to update it...

Accepted Solution

mmaxwell43534 earned 2000 total points
ID: 22947286
It sounds like the best solution here would be to use JavaScript to make an AJAX call to an ASP page that does the calculations and returns a response, i.e. In Stock/Out of Stock.

Here's the JavaScript portion.  I'll post the ASP piece next.

Basically, first you make sure they have an AJAX supporting browser.

The trimIt() function is used in case you return an XML row that is blank.  If there is no space, FireFox will error when trying to pull the data.

var ff = document.styleSheets[0].cssRules[0]; makes IE error and move to the IE friendly code.

<script language="Javascript">
	var xmlhttp = new XMLHttpRequest();
else if(window.ActiveXObject)
	try {
		var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
		try {
			var xmlhttp = new ActiveXObject("Msxml2.XMLHttp");
			alert("You're browser does not support Ajax. Please switch to Internet Explorer or FireFox in order for this page to function correctly");
function trimIt(textval) {
	textval = textval.substring(1,textval.length);
	return textval;
function validateStock() {
	var objXml = xmlhttp.responseXML;
	if (objXml.getElementsByTagName("error")[0]) {
		try {
			var ff = document.styleSheets[0].cssRules[0];
		catch(e) {
		return false;

Open in new window


Expert Comment

ID: 22947317
Here's the ASP portion.

NoSQLAttacks prevents SQL Injection attacks.

This will only tell you if you don't have anything in stock, so assume if no error, then they're good.
Dim objConnect, objCmd, objRst, id, amount, haserrors
Set objConnect = Server.CreateObject("ADODB.Connection")
objConnect.CommandTimeout = 0
objConnect.ConnectionTimeout = 0
objConnect.Open Connection_String
Set objCmd = Server.CreateObject("ADODB.Command")
objCmd.CommandTimeout = 0
Set objCmd.ActiveConnection=objConnect
Set objRst = Server.CreateObject("ADODB.Recordset")
Set objRst.ActiveConnection = objConnect
Set objRst.Source = objCmd
id = NoSQLAttacks(Request.QueryString("id"))
amount = NoSQLAttacks(Request.QueryString("amount"))
haserrors = 0
''check to see if id or amount is null
If isNull(id) OR id="" OR NOT isNumeric(id) Then
        Response.Write("<error> Item ID Not Valid.</error>" & vbCrLf)
        haserrors = 1
End If
If isNull(amount) OR amount="" or NOT isNumeric(amount) Then
        Response.Write("<error> Amount is Not Valid.</error>" & vbCrLf)
        haserrors = 1
End If
''if no errors, then do the rest
If haserrors = 0 Then
        '' Run your Query and Calculations here
        If NOT inStock = 1 Then
               Response.Write("<error> Not Enough in Stock!</error>" & vbCrLf)
        End IF
End If
Function NoSQLAttacks(fld)
	If fld="" OR isNull(fld) Then
		fld = Replace(fld,"'","")
		fld = Replace(fld,"""","")
		NoSQLAttacks = fld
	End If
End Function

Open in new window


Expert Comment

ID: 22947321
Oops, and remember to set your objects to nothing after you're done to free up memory.
Set objRst = Nothing
Set objCmd = Nothing
Set objConnect = Nothing

Open in new window


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

830 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