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>
Qty:</font>&nbsp;
<input type="text"  name="qty" size="2" value="<%=iUnits%>" maxlength="8" tabindex=1>
</td>
<td>			      			    
<a href="javascript:preprocess('0','<%=iUnits%>');">
<img height=26 src="images/newcart.jpg"  ALIGN=baseline border="0" alt="ADD TO CART"></a>
</td></tr></table>

Open in new window

ximbuexAsked:
Who is Participating?

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

x
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.

silemoneCommented:
you can either place in hidden field or  you can pass through querystring...
0
silemoneCommented:
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...
0
silemoneCommented:
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.
0
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".  

0
silemoneCommented:
ok...

<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...
0
mmaxwell43534Commented:
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">
if(window.XMLHttpRequest)
{
	var xmlhttp = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
	try {
		var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	catch(e)
	{
		try {
			var xmlhttp = new ActiveXObject("Msxml2.XMLHttp");
		}
		catch(e2)
		{
			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() {
	xmlhttp.open("GET","pagehere_val.asp?id="+document.getElementById("itemID").value+"&amount="+document.getElementById("amount").value,false);
	xmlhttp.send(null);
	var objXml = xmlhttp.responseXML;
	if (objXml.getElementsByTagName("error")[0]) {
		try {
			var ff = document.styleSheets[0].cssRules[0];
			alert(trimIt(objXml.getElementsByTagName("error")[0].firstChild.nodeValue));
		}
		catch(e) {
			alert(objXml.getElementsByTagName("error")[0].text);
		}
		return false;
	}
}
</script>

Open in new window

0

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
mmaxwell43534Commented:
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.
<%
Response.ContentType="text/xml"
Response.CacheControl="Private"
Response.Write("<xml>")
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
Response.Write("</xml>")
 
 
Function NoSQLAttacks(fld)
	If fld="" OR isNull(fld) Then
		NoSQLAttacks=fld
	Else
		fld = Replace(fld,"'","")
		fld = Replace(fld,"""","")
		NoSQLAttacks = fld
	End If
End Function
 
%>

Open in new window

0
mmaxwell43534Commented:
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

0
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
ASP

From novice to tech pro — start learning today.