Pass javascript variable to ASP variable

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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

you can either place in hidden field or  you can pass through querystring...
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...
if you're using classic asp, then same applies...except you wouldn't be using asp control...

<input type="hidden" value=""> this case you would have to create a session since on postback, the value would revert back to original value.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

ximbuexAuthor Commented:
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".  


<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 probably will have to update it...
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() {"GET","pagehere_val.asp?id="+document.getElementById("itemID").value+"&amount="+document.getElementById("amount").value,false);
	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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.