Link to home
Start Free TrialLog in
Avatar of ximbuex
ximbuex

asked on

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

Avatar of silemone
silemone
Flag of United States of America image

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="">...in this case you would have to create a session since on postback, the value would revert back to original value.
Avatar of ximbuex
ximbuex

ASKER

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

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...
ASKER CERTIFIED SOLUTION
Avatar of mmaxwell43534
mmaxwell43534
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start 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.
<%
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

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