• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1081
  • Last Modified:

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

0
ximbuex
Asked:
ximbuex
  • 4
  • 3
1 Solution
 
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
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.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now