Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Doubling up on inputs

Status
Not open for further replies.

mpwright

Technical User
Sep 13, 2004
27
GB
Hi,

Ive just moved into a new job & have been asked to modify a asp shopping cart site. I asked a few questions in the ASP part of this site & got some good advice, but the following post has left me a bit stumped as I only have some very basic html knowledge. Below is what another user has told me to do:

"You have a doubled up form input on the previous page. Anytime you get multiple values for a single input when your expecting only one value it means you have two inputs with the same name. This will cause them to get poassed as a comma-space delimited string.

I agree that you should add some isNumeric validation on the server side, but you need to go back to the previous page first and figure out why there are two inputs with the same name and get rid of the offending one that is passing the extra (unnecessary) value."

So my question is, how do I modify the HTML code to not double up on inputs, the code im using is below:

<%



Recentpage = (request.cookies("recentpage"))

if request.cookies("modified") = "true" then
reloadpage = "default.asp"
target="_parent"
else
reloadpage = "prodetails.asp"
target="main"
end if

%>

<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!-- hide JS code
var justValidating = true
function validateForm(f){
var val = f.item.value;
if(/^\d{1,}$/.test(val)){
return true;
}else{
f.item.focus;
alert('Invalid Input for quantity');
return false;
}

}


// end JS hide -->
</SCRIPT>
</head>
<body bgcolor="#FFFFFF" vlink="blue"><form METHOD="get" ACTION="qtyupd.asp" target="_parent" onsubmit="return validateForm(this)">
<div align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%">
<tr valign="top">
<td height="95%">
<div align="center">
<p><font size="5"><b><font face="Arial, Helvetica, sans-serif">Your
Shopping Cart</font></b></font></p>
<p>&nbsp;</p>
</div>
<table width="550" border="1" align="center" bordercolor="#000000" cellspacing="0" height="55">
<tr bgcolor="#333366">
<td height="25">
<table width="100%" border="0" cellspacing="0">
<tr>
<td width="310"><font size="2"><b><font face=Arial color=#FFFFFF>&nbsp;Item
Name</font></b></font></td>
<td width="90">
<div align="center"><font size="2"><b><font face=Arial color=#FFFFFF>Unit
Price</font></b></font></div>
</td>
<td width="32">
<div align="center"><font size="2"><b><font face=Arial color=#FFFFFF>Qty.</font></b></font></div>
</td>
<td width="110">
<div align="center"><font size="2"><b><font face=Arial color=#FFFFFF>Extended
Price</font></b></font></div>
</td>
</tr>
</table>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="25">
<table cellpadding=4 border=0 cellspacing=0 width='550' align="center">
<%

Set conn = Server.CreateObject("ADODB.Connection")
Conn.Open ("driver={Microsoft Access Driver (*.mdb)};DBQ=" & server.mappath("aspcart5.mdb"))

sql = "SELECT * FROM temporary where custid = '" & Request.Cookies("customerid") & "' ORDER BY item;"
set rs = Conn.Execute(sql)

do while not rs.eof

spaceditem = Replace((rs("item"))," ","+")

runningtotal = runningtotal + rs("unitprice") * rs("quantity")
runningqty = runningqty + rs("quantity") * 1

stripped = Replace(rs("item"),"_"," ")
secondstripped = Replace(stripped,"^","'")
%>
<tr>
<td bgcolor=#FFFFFF width="46%"><font face="Arial, Helvetica, sans-serif" size="2"><a href="reverseget.asp?reverseget=<%=secondstripped%>&start=1"><%=secondstripped%></a></font></td>
<td bgcolor=#FFFFFF width="11%"> <font face="Arial, Helvetica, sans-serif" size="2"><a href="delitem.asp?item=<%=rs("item")%>" target="_parent"><img src="delete.jpg" height="15" vspace="0" hspace="0" border="0"></a>
</font></td>
<td align=RIGHT bgcolor=#FFFFFF width="17%">
<div align="center"><font face="Arial, Helvetica, sans-serif" size="2"><%=FormatCurrency(rs("unitprice"))%></font></div>
</td>
<td align=RIGHT bgcolor=#FFFFFF width="6%">
<div align="center"><font face="Arial, Helvetica, sans-serif" size="2">
<input size=2 maxlength=5 name="item" value="<%=rs("quantity")%>">
</font></div>
</td>
<td align=RIGHT bgcolor=#FFFFFF width="20%">
<div align="center"><font face="Arial, Helvetica, sans-serif" size="2"><%=FormatCurrency(rs("quantity") * rs("unitprice"))%></font></div>
</td>
</tr>
<%
rs.movenext
loop
rs.close

If runningtotal = 0 then %>
<tr>
<td align='LEFT' colspan='5' gcolor='#EEEEEE' height="2" bgcolor="#CCCCCC">
<div align="center"><b><font face="Arial, Helvetica, sans-serif">Your
Shopping Cart is Empty</font></b></div>
</td>
</tr>
<% End if %>
<tr bgcolor="#CCCCCC">
<td align='LEFT' colspan='3' gcolor='#EEEEEE' height="33" bgcolor="#FFFFFF"><font face="Arial, Helvetica, sans-serif" size="2"><b>Total,
Less Tax and Shipping &amp; Handling:</b></font></td>
<td align='RIGHT' width="6%" height="33">
<div align="center"><b><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><%=runningqty%></font></b></div>
</td>
<td align='RIGHT' width="20%" height="33">
<div align="center"><b><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><%=FormatCurrency(runningtotal)%>&nbsp;</font></b></div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<div align="center">
<p>&nbsp;</p>
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<div align="center"><a href="continue.asp?prodid=<%=Request.Cookies("lastprod")%>" target="_parent"><img src="return.gif" alt="CONTINUE SHOPPING" border="0" width="93" height="35"></a></div>
</td>
<td>
<div align="center">
<input type="IMAGE" src="update.gif" border="0" name="UPDATE QUANTITIES" width="93" height="35">
</div>
</td>
<td>
<div align="center"><a href="emptycart.asp" target="_parent"><img src="clear.gif" alt="EMPTY CART" border="0" width="93" height="35"></a></div>
</td>
<td>
<div align="center"><a href="checkout.asp"><img src="checkout.gif" alt="CHECKOUT" border="0" width="93" height="35"></a></div>
</td>
</tr>
</table>
</div>

</form>
</body></html>

Any advice would be greatly appriciated.

Kind Regards

Mark
 
Ok, you're running a loop in which you write out this line:

<input size=2 maxlength=5 name="item" value="<%=rs("quantity")%>">

This creates an input box with a name item and if you have many items in a loop, it creates many input boxes with the same name. Add some counting logic to your ASP code and produce the name of the input box as name="item<%=counter%>". Increase counter with every loop and you will have input boxes with unique names. Hope it makes sense.
 
ok, so instead of using:

<input size=2 maxlength=5 name="item" value="<%=rs("quantity")%>">

I need to use:

<input size=2 maxlength=5 name="item<%=counter%> value="<%=rs("quantity")%>">

Is that right? Also how do I add counting logic into the asp code? Ive never really used asp before but from what I can work out this is the code that the input box refers to:

<%

input1=Request.QueryString("quantity")

input1 = Csng(input1)

If VarType(input1) <> 4 then

Response.Write("<font face=""Verdana""><b>Error:</b> the quantity value entered must be in numeric form. The item was not added to your basket.")

Else



If Request.Cookies("customerid") = "" then

Set conn = Server.CreateObject("ADODB.Connection")
Conn.Open ("driver={Microsoft Access Driver (*.mdb)};DBQ=" & server.mappath("aspcart5.mdb"))

sql = "Select dptid from idtable"

set rs = conn.execute(sql)

newcustid = rs("dptid")

sql = "Update idtable set dptid = " & newcustid + 1

conn.execute(sql)

set conn = nothing

Response.Cookies("customerid") = newcustid

End if


Set conn = Server.CreateObject("ADODB.Connection")
Conn.Open ("driver={Microsoft Access Driver (*.mdb)};DBQ=" & server.mappath("aspcart5.mdb"))


If Request.QueryString("custom") = "yes" then
CUSTitem = Request.QueryString("item") & ", " & Request.QueryString("custom1")
If Request.QueryString("custom2") <> "" then CUSTitem = CUSTitem & ", " & Request.QueryString("custom2")
If Request.QueryString("custom3") <> "" then CUSTitem = CUSTitem & ", " & Request.QueryString("custom3")
Else
CUSTitem = Request.QueryString("item")
End If

CUSTqty = Request.QueryString("quantity")
CUSTprice = Request.QueryString("price")
customerid = Request.Cookies("customerid")
expiretime = formatnumber(date,0,-1,-1,0)

sql = "SELECT item FROM temporary where custid = '" & Request.Cookies("customerid") & "' ORDER BY item;"
set rs = Conn.Execute(sql)

do while not rs.eof

If rs("item") = CUSTitem then Foundit = "Yes"
rs.movenext
loop

If Foundit <> "Yes" then
sql = "INSERT INTO temporary (custID, item, unitprice, quantity, expire) VALUES ('" + customerid + "', '" + CUSTitem + "', '" + CUSTprice + "', '" + CUSTqty + "', " + expiretime + ")"
set rs = Conn.Execute(sql)
End if

If Foundit = "Yes" then

sql = "SELECT * FROM temporary where custid = '" & Request.Cookies("customerid") & "' And item = '" & CUSTitem & "'"
set rs = Conn.Execute(sql)

existingQTY = rs("quantity") * 1 + request.querystring("quantity")

sql = "UPDATE DISTINCTROW temporary SET quantity ='" & existingQTY & "' WHERE item='" + CUSTitem + "' AND custID='" + customerid + "'"
set rs = Conn.Execute(sql)
End if

set rs = nothing

Response.Redirect("default.asp?start=" & Request.QueryString("start") & "&page=" & Request.QueryString("department"))

End If


%>


Many Thanks for your help
 
Make changes somewhre around here:
Code:
counter = 1

do while not rs.eof

    If rs("item") = CUSTitem then Foundit = "Yes"    
    rs.movenext
    counter = counter + 1
loop
Input should look something like this:
Code:
<input size="2" maxlength="5" name="item<%=counter%>" value="<%=rs("quantity")%>" />
This way, when you have more input boxes on the page, they will look like this:
Code:
<input size="2" maxlength="5" name="item1" value="1" />
<input size="2" maxlength="5" name="item2" value="1" />
<input size="2" maxlength="5" name="item3" value="1" />
<input size="2" maxlength="5" name="item4" value="1" />
I am not too sure about ASP code, it has been awhile since I done VBScript.
 
when i put the above code in I get the following:

Error Type:
(0x80020009)
Exception occurred.
/asp/qtyupd.asp, line 25

Im not sure where abouts I need to put your code (the counter = 1 code) is it in the first bit of code I pasted or the second?, does it replace something thats already in there or is it a piece of code that can be put anywhere?

Many thanks
 
Ignore my last post, ive found where I need to put it. But when I try to update the quantities box it freezes the browser and eventually displays:

Error Type:
Active Server Pages, ASP 0113 (0x80004005)
The maximum amount of time for a script to execute was exceeded. You can change this limit by specifying a new value for the property Server.ScriptTimeout or by changing the value in the IIS administration tools.
/asp/qtyupd.asp
 
Well, ive been playing about with the above code but it now just freezes completley whenever I try to update the shopping cart, even if I only have one item in there. Does anyone have any ideas?

Cheers

Mark
 
Right im getting there, ive now sorted out the doubling up on inputs problem by putting the following in viewcart.asp:

<input size=2 maxlength=5 name="item<%=counter%>" value="<%=rs("quantity")%>">

</font></div>
</td>
<td align=RIGHT bgcolor=#FFFFFF width="20%">
<div align="center"><font face="Arial, Helvetica, sans-serif" size="2"><%=FormatCurrency(rs("quantity") * rs("unitprice"))%></font></div>
</td>
</tr>
<%
rs.movenext
counter = counter + 1
loop
rs.close
conn.close
set rs = nothing
set conn = nothing

If runningtotal <> 0 then%>

and then this as the valadation script:
<script type="text/JavaScript">
function validateForm(f)
{
for (var i=0; i<f.elements.length; i++)
{
if(isNaN(document.getElementById('item'+i).Value))
{
alert("Invalid Quantity on item " + i);
}
}
else
{ return true; }
}
</script>

this should check multiple inputs right? But when i now try to update the quantity in the car it just empties it? Any ideas?

Regards

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top