View Full Version : selecting and display values from data bases choosen by user.form
Thimo
10-23-2002, 03:24 AM
hi,
View Items Page
<form name="form1" method="post" action="AddShoppingcart.asp">
<br>
<table border="1" align="center">
<tr>
<th>Item Code</th>
<th>ItemName</th>
<th>Quantity</th>
<th>Price</th>
<th>Click to Add</th>
</tr>
<%Do While Not rsItems.EOF %>
<tr align=center>
<td><%=rsItems("ItemCode")%></td>
<td align=left><%=rsItems("ItemName")%></td>
<td><%=rsItems("Quantity")%> </td>
<td><%=FormatCurrency(rsItems("Price"))%> </td>
<td><input type="checkbox" name="add" value=<%=rsItems("ItemID")%>></td>
<tr>
<%rsItems.MoveNext%>
<%loop%>
<%End If%>
</table>
<p>
<input type="submit" name="Submit" value="Add Items to Cart">
</p>
</form>
Addshoppingcart.asp
<%
Dim numpdts, i, rs, total
Set rs = Server.CreateObject("ADODB.Recordset")
numpdts = Request.Form("add").Count
strSQL = "SELECT * FROM Item WHERE ItemID="
For i=1 to numpdts
Rem -- Append the product ID to the SQL statement and execute it.
total = (strSQL & Request.Form("add")(i))
Next
rs.Open total,strConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
%>
<html>
<body>
<table border="1" align="center">
<tr>
<th>Item Code</th>
<th>ItemName</th>
<th>Quantity</th>
<th>Price</th>
<th>Click to Add</th>
</tr>
<%Do While Not rs.EOF %>
<tr align=center>
<td><%=rs("ItemCode")%></td>
<td align=left><%=rs("ItemName")%></td>
<td><%=rs("Quantity")%> </td>
<td><%=FormatCurrency(rs("Price"))%> </td>
<tr>
<%rs.MoveNext%>
<%loop%>
</table>
</body>
</html>
sorry for the codes, user at ViewItem page can select eg, 5 items by checking their boxes, then submitted to the next page, at the Addshoppingcart.asp, i use a FOR Loop to loop through all the 5 records for it to display, but it seems that i have mixed up withe the Command object and Recordset object properties and used it wrongly..... eg. item 1 to item 5 is checked by only item 5 is displayed at the page...
please advice
thanks
glenngv
10-23-2002, 03:54 AM
for each item in Request.Form("add")
if whereClause<>"" then
whereClause = whereClause & " OR ItemID=" & item
else
whereClause = "ItemID=" & item
end if
next
strSQL = "SELECT * FROM Item WHERE " & whereClause
i assume ItemID's datatype is numeric that's why I didnt enclose it in single quotes. If it is varchar, then you should put single quotes:
Thimo
10-23-2002, 04:01 AM
Originally posted by glenngv
for each item in Request.Form("add")
if whereClause<>"" then
whereClause = whereClause & " OR ItemID=" & item
else
whereClause = "ItemID=" & item
end if
next
strSQL = "SELECT * FROM Item WHERE " & whereClause
i assume ItemID's datatype is numeric that's why I didnt enclose it in single quotes. If it is varchar, then you should put single quotes:
ITEMID datatype is Autonumber, does it matter?......
anyway thanks...alot...i will try to debug again...please stay ard to help.....kekekeke
thanks again
erm..sorry to disturb you again..... can briefly explain the logic and can i use the Recordset object to display the fields?
glenngv
10-23-2002, 04:07 AM
then you could use the code i posted as it is
have you tried it? does it work?
Thimo
10-23-2002, 04:10 AM
Originally posted by glenngv
then you could use the code i posted as it is
have you tried it? does it work?
<%
Dim whereClause, rs
For each item in Request.Form("add")
if whereClause <> "" then
whereClause = whereClause & "OR ItemID=" & item
else
whereClause = "ItemID=" & item
end if
next
strSQL = "SELECT * FROM Item WHERE " & whereClause
rs.Open strSQL, objConn ----->>>> line 18
%>
<html>
<body>
<table border="1" align="center">
<tr>
<th>Item Code</th>
<th>ItemName</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<%Do While Not rs.EOF %>
<tr align=center>
<td><%=rs("ItemCode")%></td>
<td align=left><%=rs("ItemName")%></td>
<td><%=rs("Quantity")%> </td>
<td><%=FormatCurrency(rs("Price"))%> </td>
<tr>
<%rs.MoveNext%>
<%loop%>
</table>
</body>
</html>
i changed it to this?...izzit correct? but got error say
Object required * at line 18
Thimo
10-23-2002, 04:12 AM
can already....
so sorry i forgot to add the set rs= objConn.execute(strsql)
lolx....
thanks alot...man...will you be always ard here?...i might have more questions in near future....
Thimo
10-23-2002, 04:18 AM
for each item in Request.Form("add")
if whereClause<>"" then
whereClause = whereClause & " OR ItemID=" & item
else
whereClause = "ItemID=" & item
end if
next
strSQL = "SELECT * FROM Item WHERE " & whereClause
i wish to understand one thing, for the (for each item in Request.Form("add")), its is used this way? means i can pass alot of values into a Request.Form?
so the item is the numbers of value in "add"?
glenngv
10-23-2002, 04:24 AM
since all your checkboxes have the same name "add", if you do request.form("add"), it is a collection.
response.write "add: " & request.form("add")
if you write its value, you can see:
add: 1,3,4,5
which are the values of the checkboxes that are checked.
you can access each item by doing for each statement.
Thimo
10-23-2002, 04:27 AM
thanks alot man,,,,,,
so far i have done to retrieve items to display at the user shopping cart.... i now need to try to insert these items into the Database for records....
when i have problems , can you help?....
thanks and regards
glenngv
10-23-2002, 04:40 AM
if you encounter any problem, just post it and definitely all the people here in this forum are willing to help. :)
whammy
10-24-2002, 01:18 AM
Yup. Like glenn said, when you have a radio button or checkbox on the previous page (or for that matter any two fields, regardless of their type) that have the same name, any "additional" values (more than one) will basically turn it into a comma-delimited array when you "post" it to the next page.
In those cases, I usually first check to see if there is a comma in the passed variable...
If InStr(variablename,",") Then
'parse variablename as an array
Else
'treat it as a single variable
End If
I hope that helps. :D
Thimo
10-24-2002, 02:56 AM
another question here,
how come i cannot compare like <%rs("ItemQuantity")%> with the variable i created in the script tag, i would like to use the function like MsgBox to display like "Requested Quantity too much"
like for my Addshoppingcart.asp, i manged to display all the items and the quantites available, so at this page i allow them to key in their requested quantity
form name="form1" method="post" action="AddToCart.asp" >
<table border="1" align="center">
<tr>
<th height="25">Item Code</th>
<th>ItemName</th>
<th>Quantity</th>
<th>Price</th>
<th>Request Quantity</th>
</tr>
<%Do While Not rs.EOF %>
<tr align=center>
<td><%=rs("ItemCode")%></td>
<td align=left><%=rs("ItemName")%></td>
<td><%=rs("Quantity")%> </td>
<td><%=FormatCurrency(rs("Price"))%> </td><td><input name="RQty" type="text" size="3" maxlength="3" ></td>
<tr>
<%rs.MoveNext%>
<%loop%>
</table>
the form actions to AddTocart.asp, i can't seem to use Javascript or Vbscript functions for like form validation....please advice...
like compare quantity.
<%
'setting recordset and SQL statement
If Request("Qty") > rs("ItemQuantity")
Msgbox "Quantity not enough!!"
%>
i cannto do this right?...
whammy
10-24-2002, 11:54 PM
That's because anything in the Request collection is treated as a string unless you change its subtype:
If IsNumeric(Request("Qty")) Then 'or validate it against a currency regular expression...
If CDbl(Request("Qty")) > rs("ItemQuantity") 'assuming that ItemQuantity is a float being returned as a double
Msgbox "Quantity not enough!!"
End If
End If
Hope this helps. :D
glenngv
10-25-2002, 02:19 AM
just a note, MsgBox is only used in client-side VbScript not on server-side. you'll get a permission denied error.
whammy
10-25-2002, 02:40 AM
I figured he knew that if he's been using it... as obviously you can't depend on VBScript itself for webpages (which is why this forum is named "javascript"kit.com).
If more information is needed check out:
http://www.w3schools.com
:D
Thimo
10-31-2002, 06:39 AM
Originally posted by whammy
Yup. Like glenn said, when you have a radio button or checkbox on the previous page (or for that matter any two fields, regardless of their type) that have the same name, any "additional" values (more than one) will basically turn it into a comma-delimited array when you "post" it to the next page.
In those cases, I usually first check to see if there is a comma in the passed variable...
If InStr(variablename,",") Then
'parse variablename as an array
Else
'treat it as a single variable
End If
I hope that helps. :D
how do i spilt the array ? like for example for my application
if the user checked Item 1 and 10
the next page will appear like this
Item 1 - ItemName - Price - Quantity available - Request Quanity
Item 10 - ItemName - Price - Quantity available - Request Quanity
the Request Quantity is a text box name = "RQty"
i know when i do a Request.Form("RQty") its will be a comma delimited array right....how do i spilt the array to allocate the value corressponding to the itemID the user wants.....
please advise thanks
whammy
10-31-2002, 11:51 PM
Well one way:
<input type="checkbox" name="mycheckbox" value="<% = Item1 %>|<% = ItemName%>|<% = Price%>|<% = QuantityAvailable %>|<% = RequestQuanity %>" />
Then, when you pass the checkboxes to the next page, they will end up something like:
mystring= "Item1|Foobar|3.25|13|2,Item11|Barfoo|10.00|25|1"
Of course, then you can split it twice, once by the comma and then by the pipe.
myarray = Split(mystring,",")
after that, you'd have:
myarray(0) = "Item1|Foobar|3.25|13|2"
myarray(1) = "Item11|Barfoo|10.00|25|1"
Just by using split.
Then you can split each part of myarray():
For i = 0 to UBound(myarray)
temparray = Split(myarray(i),"|")
For x = 0 to UBound(temparray)
Response.Write(temparray(x) & "<br />" & vbCrLf)
Next
Next
:)
Thimo
11-01-2002, 05:28 AM
i have an error prompt
Type Mismatch : Ubound
what does this UBound means? oie i get it....it retrurns the most number of values in an array right?.....
but how i come i got his Type MIsmatch...
please advice..
Thimo
11-01-2002, 06:05 AM
<!--#include file = "adoObjects.asp"-->
<%
If Session("PersonID")= "" Then
Response.Redirect "Login.asp?again=True"
End If
%>
<html>
<head>
<title>Display Items Order</title>
</head>
<body>
<%
Dim whereClause, rs
For Each Item in Request.Form("add")
If whereClause <> "" then
whereClause = whereClause & "OR ItemID=" & item
Else
whereClause = "ItemID=" & item
End If
Next
strSQL = "SELECT * FROM Item WHERE " & whereClause
set rs = objConn.Execute(strSQL)
dim i, myarray, temparray, x
myarray = Request("RQty")
For i = 0 to UBound(myarray) ---->>>>>> Error
temparray = Split(myarray(i),",")
For x = 0 to UBound(temparray)
Response.Write(temparray(x) & "<br />" & vbCrLf)
Next
Next
%>
<form>
<table border="1" align="center">
<tr>
<th height="25">Item Code</th>
<th>ItemName</th>
<th>Price</th>
<th>Request Quantity</th>
</tr>
<%Do While Not rs.EOF%>
<tr align=center>
<td><%=rs("ItemCode")%></td>
<td align=left><%=rs("ItemName")%></td>
<td><%=temparray></td>
<td><%=FormatCurrency(rs("Price"))%></td>
</tr>
<%rs.MoveNext%>
<%loop%>
</table>
</form>
</body>
</html>
<%Session.Abandon%>
i think you should take a look at my codes...... i managed to spilt the array already.... but i cannot manage tto link it with the corresponing Item the user wants.....
and the line that has error, its seems i have to spilt the array before i use UBound...func...
when i use only the For loop inside which is the for x = 0, i am able to display the spilted array...
please advice thanks....haiz....i am down with rashes...even to my eyes..that y cannot think properly...please help...
whammy
11-01-2002, 10:00 AM
That means there was nothing to split it by and the array was not created. Which is why I suggested this:
If InStr(variablename,",") Then
'parse variablename as an array
Else
'treat it as a single variable
End If
to start with. :)
Thimo
11-01-2002, 04:08 PM
thanks...alot man....
i will go and try this weekend........ if any problems i post again here?....thanks
regards
Thimo
11-15-2002, 06:50 AM
whammy? thanks for the previous solution...i manage to get the array to pass to another page...
but there is a new problem...
you know when the user 10 items...okie the next page will show its price and quantities and i used JS to validate for errors inputs by the user... its works perfectly...
the only problem now is when the user only SELECTS one item... and the JS juz couldn;t validate the array
<%
Dim whereClause, rs, myarray
myarray = Request.Form("add")
If InStr(myarray,",") Then
For Each Item in Request.Form("add")
If whereClause <> "" then
whereClause = whereClause & "OR ItemID=" & item
Else
whereClause = "ItemID=" & item
End If
Next
strSQL = "SELECT * FROM Item WHERE " & whereClause
Else
strSQL = "SELECT * FROM Item WHERE ItemID=" & Request.Form("add")
End If
set rs = objConn.Execute(strSQL)
%>
<basefont face="Comic Sans Ms" color="Black">
<html>
<head>
<title>REQUEST QUANTITY</title>
<script language="JavaScript">
<!--
function VerifyQty(f)
{
var flag=0;
var i;
/*for (i=0;i<f.RQty.length;i++)
{
var quantityarray = parseFloat(f.RQty[i].value);
var stockarray = parseFloat(f.Qty[i].value);
if (quantityarray == "" || 0)
flag=1;
else if ( isNaN(f.RQty[i].value.charAt(0)) == true || isNaN (f.RQty[i].value.charAt(1)) == true || isNaN(f.RQty[i].value.charAt(2)) == true )
flag=3;
else if(quantityarray > stockarray)
flag=2;
}
document.write (f.RQty.length)
f(flag==1)
alert("Please enter Quantity");
else if(flag==3)
alert("Invalid quantity")
else if(flag==2)
alert("Quantity you have entered is above the stock available...!!!");
else
{
var confirm2 = confirm ("Are you sure to submit your selection?" + "\n" + "Once submitted, your order is finalised")
if (confirm2==true)
f.submit();
else
alert("please confirm your selection...")
}
}
-->
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
<body bgcolor="#FFCCFF" background="../images/paper_b011.gif">
<center>
<h1><i><span style="letter-spacing: 1pt"><b><font face="Impact" size="5"><sup><font size="+7">the
</font></sup></font><font face="Impact" size="+7">E-BookStore</font></b></span></i><br>
Stationery Item Menu</h1>
<h3>Welcome <%= Session("PersonName")%></h3>
<p align="center"><strong>Instructions : Enter stationery quantities and click
REQUEST to request for items.</strong></p>
</center>
<div align="center">
<img src="../images/101.gif" width="437" height="46"> </div>
<form name="cart_form" method="post" action="FinishCart.asp" >
<table border="1" align="center">
<tr>
<th height="25">Item Code</th>
<th>ItemName</th>
<th>Quantity</th>
<th>Price</th>
<th>Request Quantity</th>
</tr>
<%Do While Not rs.EOF%>
<tr align=center>
<td><%=rs("ItemCode")%></td>
<td align=left><%=rs("ItemName")%></td>
<td><%=rs("Quantity")%></td>
<td><%=FormatCurrency(rs("Price"))%></td>
<td><input type="text" name="RQty" size="5" maxlength="4" ></td>
<input type ="hidden" name="Qty" value="<%=rs("Quantity")%>">
<input type ="hidden" name="add" value="<%=rs("ItemID")%>">
<tr>
<%rs.MoveNext%>
<%loop%>
</table>
<p> </p>
<center>
<input type="button" name="Submit" value="Submit" onClick="VerifyQty(cart_form)">
<input type="reset" name="Reset">
</center>
</form>
i did a document.write on f.RQty.length , and its wrote "undefined" izzit because one item is passed in and not considered an array? so the length is 0? please advice..
thanks.....
glenngv
11-15-2002, 08:02 AM
first, you should pass the form object like this:
<input type="button" name="Submit" value="Submit" onClick="VerifyQty(document.cart_form)">
but you can simply do it like this:
<input type="button" name="Submit" value="Submit" onClick="VerifyQty(this.form)">
second, since RQty may or may not be present in the form, and if it's present, it could be 1 or more items, then you should do it like this:
function VerifyQty(f)
{
var flag=0;
var i;
if (f.RQty){ //RQTY exists!
if (f.RQty.length) { //RQty is more than 1
}
else {//RQty is only 1
}
}
...
glenngv
11-15-2002, 08:39 AM
Originally posted by whammy
That means there was nothing to split it by and the array was not created. Which is why I suggested this:
If InStr(variablename,",") Then
'parse variablename as an array
Else
'treat it as a single variable
End If
to start with. :)
split() method always return an array even if the specified string is empty or the delimeter is found or not.
take this simple example:
Sub mySplit(ByVal str)
arr = split(str,",")
response.write "<p>String: " & str & "<br>"
response.write "IsArray? " & isarray(arr) & "<br>"
response.write "Ubound: " & ubound(arr) & "<br>"
for i=0 to ubound(arr)
response.write i & ": " & arr(i) & "<br>"
next
End Sub
mySplit("")
mySplit("Item1")
mySplit("Item1,Item2")
the output would be:
String:
IsArray? True
Ubound: -1
String: Item1
IsArray? True
Ubound: 0
0: Item1
String: Item1,Item2
IsArray? True
Ubound: 1
0: Item1
1: Item2
so we should always check if Ubound(arrayName) is not -1 to see a "valid" array.
whammy
11-16-2002, 12:25 AM
Sweet. That wasn't covered in my book. Or in any code I had handy... :)
I will remember that.
Thimo
11-16-2002, 07:43 AM
thanks glenn..for the advice...
i managed to get the thing solved ......juz now....kekek..thanks alot man...
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.