passing values in jsp.
String[] numbername ={"1","2","3"};
String numval[] = new String[3];
for(int j=0; j<names.length; j++){ %>
<TR><TD BGCOLOR=666699 ALIGN=CENTER><b><%=j+1%>.</b></TD>
<TD BGCOLOR=AAAAAA ALIGN=CENTER> <font class="name"><%=names[j]%></font> </TD>
<TD BGCOLOR=AAAAAA ALIGN=CENTER width="9%"><font class="cname"><%=Cnames[j]%></font></TD>
<TD BGCOLOR=AAAAAA ALIGN=CENTER><font class="description"><%=SKUs[j]%></font></TD>
<TD BGCOLOR=AAAAAA ALIGN=CENTER>Squab</TD>
<TD BGCOLOR=AAAAAA ALIGN=CENTER><b>$<%=prices[j]%>0</b></TD><!--value=shoppingCart.setQty(Integer.parseInt(numval[j]))%>-->
<TD BGCOLOR=AAAAAA ALIGN=CENTER><input type="text" name=<%=numbername[j]%> size="2" > </TD>
<TD BGCOLOR=cccc66>
<A HREF="squab1.jsp?name=<%=names[j]%>&cname=<%=Cnames[j]%>&sku=<%=SKUs[j]%>&price=<%=prices[j]%>&numname=<%=numbername[j]%>">
Add</A></TD></TR>
<% } %>
This is a part of my code for a shoppingcart. I have been trying to get the value from the text box which is the quantity for this cart. - <input type="text" name=<%=numbername[j]%> size="2" > But the values are static from this page. When I enter the values from the page, it did not give the values i enter instead the static values appears. Can anyone know a way to get the value as entered in this for loops? Please help by take a look at this jsp code above. I have been trying many ways in many days but no answere. Can anyone help?
Can you explain this in a little more detail. I can help you out but I need more information on what it is you are trying to do. That and a larger chunck of code would help. I see variables that you are using but dont know how you have created them which may be important.
bobd3 at 2007-6-29 10:01:07 >

When you submit the the page hope you are getting the numbername values as request.getParameter().
If so then the static initiation what you have done of this string array should be done before the assigning values to it from the request.getParameter..
That will probably solve the problem.
Dipanc at 2007-6-29 10:01:07 >

<HTML><HEAD><TITLE>Squab</TITLE>
<%@ page contentType="text/html; charset=Big5"%>
<%@ include file="411.css" %>
<%@ page language="java" import="myclasses.*" %>
<jsp:useBean id="shoppingCart" class="myclasses.ShoppingCarts1" scope="session"/>
<%@ page import="java.util.*" %>
</HEAD><BODY BGCOLOR=D1D1D1>
<H2 align="center"> MENU</H2>
<TABLE width="100%" cellspacing="0" ><TR>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="appetizers.jsp">Appetizers Y i</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="soup.jsp">Soup</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="abalone.jsp">Abalone j </A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="lobster.jsp">Lobster s </A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="shrimp.jsp">Shrimp </A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="crab.jsp">Crab </A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="fish.jsp">Fish </A> </TD>
</TR></TABLE>
<TABLE width="100%" cellspacing="0" cellpadding="2"><TH height="2"><TR >
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="scallops.jsp">Scallops a l</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="oysters.jsp">Oysters</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="clams.jsp">Clams</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="squid.jsp">Squid A {</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="duck.jsp">Duck n</A> </TD>
<TD BGCOLOR=666699 ALIGN=CENTER> Squab F </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="pork.jsp">Pork </A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="beef.jsp">Beef </A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="chicken.jsp">Chicken</A> </TD>
</TR></TH></TABLE>
<TABLE width="100%" cellspacing="0"><TH height="2"><TR>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="vegetarian.jsp">Vegetarian</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="cantoneseclaypot.jsp">Cantonese Clay Pot s J </A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="mu-shu.jsp">Mu-Shu</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="noodles.jsp">Noodles, </A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="friedrice.jsp">Fried Rice</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="familydinners.jsp">Family Dinners</A> </TD>
</TR></TH></TABLE>
<hr size="3">
<H2 align="left"> Squab F</H2>
<FORM ACTION=ViewCart1.jsp METHOD=get>
<TABLE width="100%">
<b><A HREF=ViewCart1.jsp> <p align="right">View Orders</p></A></b>
<TR><TD BGCOLOR=666699 ALIGN=CENTER><font class="Label">Items</font></TD>
<TD BGCOLOR=666699 ALIGN=CENTER colspan="2"><font class="Label">Name</font></TD>
<TD BGCOLOR=666699 ALIGN=CENTER><b>Decription</b></TD>
<TD BGCOLOR=666699 ALIGN=CENTER><b>Catagory</b></TD>
<TD BGCOLOR=666699 ALIGN=CENTER><b>Price</b></TD>
<TD BGCOLOR=666699 ALIGN=CENTER><b>Qty</b></TD>
<TD BGCOLOR=666699 ALIGN=CENTER> </TD></TR>
<% String[] names = {"CRISPY BRAISED SQUAB","SOY SAUCE SQUAB","PAN FRIED MINCED SQUAB WITH LETTUCE"};
String[] SKUs = {"Squab marinated in five spice seasoning and roasted to prefection.","Whole squab braised with supreme soy sauce.","Stir fried minced squab with ground water chestnuts, Chinese sausage, Chinese mushrooms and served with fresh lettuce cups and hoi sin sauce." };
String[] Cnames ={" N F"," oF","F ]"};
double[] prices = {12.00, 12.00, 12.00};
String[] numbername ={"1","1","1"};
String numval[] = new String[3];
for(int j=0; j<names.length; j++){ %>
<TR><TD BGCOLOR=666699 ALIGN=CENTER><b><%=j+1%>.</b></TD>
<TD BGCOLOR=AAAAAA ALIGN=CENTER> <font class="name"><%=names[j]%></font> </TD>
<TD BGCOLOR=AAAAAA ALIGN=CENTER width="9%"><font class="cname"><%=Cnames[j]%></font></TD>
<TD BGCOLOR=AAAAAA ALIGN=CENTER><font class="description"><%=SKUs[j]%></font></TD>
<TD BGCOLOR=AAAAAA ALIGN=CENTER>Squab F</TD>
<TD BGCOLOR=AAAAAA ALIGN=CENTER><b>$<%=prices[j]%>0</b></TD><!--value=shoppingCart.setQty(Integer.parseInt(numval[j]))%>-->
<TD BGCOLOR=AAAAAA ALIGN=CENTER><input type="text" name=<%=numbername[j]%> size="2" > </TD>
<TD BGCOLOR=cccc66>
<A HREF="squab1.jsp?name=<%=names[j]%>&cname=<%=Cnames[j]%>&sku=<%=SKUs[j]%>&price=<%=prices[j]%>&numname=<%=numbername[j]%>">
Add</A></TD></TR>
<% } %>
<TR><TD BGCOLOR=666699 ALIGN=CENTER> </TD>
<TD BGCOLOR=666699 ALIGN=CENTER colspan="2"> </TD>
<TD BGCOLOR=666699 ALIGN=CENTER> </TD>
<TD BGCOLOR=666699 ALIGN=CENTER> </TD>
<TD BGCOLOR=666699 ALIGN=CENTER> </TD>
<TD BGCOLOR=666699 ALIGN=CENTER> </TD>
<TD BGCOLOR=666699 ALIGN=CENTER> </TD>
</TR>
</TABLE>
<b> <p align="center"><input type="submit" name="submit" value="View Orders"></p></b>
<%
String name = request.getParameter("name");
String cname = request.getParameter("cname");
if(name!=null){
if(name.equals("emptyCart")){
shoppingCart.emptyCart();
} else {
String sku = request.getParameter("sku");
double price = Double.parseDouble(request.getParameter("price"));
int qty = Integer.parseInt(request.getParameter( "numname"));
shoppingCart.setQty(qty);
}
ShoppingCarts1 newItem = new ShoppingCarts1(sku, name,cname, "Squab", price);
shoppingCart.addItem(newItem);
}} %>
</FORM></BODY></HTML>
Here is the code that I am using. The only problem is that when I entered the quantity of the order , it did not take my entered number instead the static integer I assigned earlier; String[] numbername ={"1","1","1"}; located in the middle of this code. See I have to assign all quantity to 1 to all the products initially. But If some one want more than one, that person has to type the number on the text box. How can I retrieve that number instead of one. I generate all the numbers inside of the for loop. Please take a closer look at the middle part of the assigned code. Very urgent, thanks ahead.
<HTML><HEAD><TITLE>Squab</TITLE>
<%@ page contentType="text/html; charset=Big5"%>
<%@ include file="411.css" %>
<%@ page language="java" import="myclasses.*" %>
<jsp:useBean id="shoppingCart" class="myclasses.ShoppingCarts1" scope="session"/>
<%@ page import="java.util.*" %>
</HEAD><BODY BGCOLOR=D1D1D1>
<H2 align="center"> MENU</H2>
<TABLE width="100%" cellspacing="0" ><TR>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="appetizers.jsp">Appetizers Y i</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="soup.jsp">Soup</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="abalone.jsp">Abalone j </A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="lobster.jsp">Lobster s </A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="shrimp.jsp">Shrimp </A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="crab.jsp">Crab </A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="fish.jsp">Fish </A> </TD>
</TR></TABLE>
<TABLE width="100%" cellspacing="0" cellpadding="2"><TH height="2"><TR >
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="scallops.jsp">Scallops a l</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="oysters.jsp">Oysters</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="clams.jsp">Clams</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="squid.jsp">Squid A {</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="duck.jsp">Duck n</A> </TD>
<TD BGCOLOR=666699 ALIGN=CENTER> Squab F </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="pork.jsp">Pork </A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="beef.jsp">Beef </A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="chicken.jsp">Chicken</A> </TD>
</TR></TH></TABLE>
<TABLE width="100%" cellspacing="0"><TH height="2"><TR>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="vegetarian.jsp">Vegetarian</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="cantoneseclaypot.jsp">Cantonese Clay Pot s J </A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="mu-shu.jsp">Mu-Shu</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="noodles.jsp">Noodles, </A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="friedrice.jsp">Fried Rice</A> </TD>
<TD BGCOLOR=999966 ALIGN=CENTER><A HREF="familydinners.jsp">Family Dinners</A> </TD>
</TR></TH></TABLE>
<hr size="3">
<H2 align="left"> Squab F</H2>
<FORM ACTION=ViewCart1.jsp METHOD=get>
<TABLE width="100%">
<b><A HREF=ViewCart1.jsp> <p align="right">View Orders</p></A></b>
<TR><TD BGCOLOR=666699 ALIGN=CENTER><font class="Label">Items</font></TD>
<TD BGCOLOR=666699 ALIGN=CENTER colspan="2"><font class="Label">Name</font></TD>
<TD BGCOLOR=666699 ALIGN=CENTER><b>Decription</b></TD>
<TD BGCOLOR=666699 ALIGN=CENTER><b>Catagory</b></TD>
<TD BGCOLOR=666699 ALIGN=CENTER><b>Price</b></TD>
<TD BGCOLOR=666699 ALIGN=CENTER><b>Qty</b></TD>
<TD BGCOLOR=666699 ALIGN=CENTER> </TD></TR>
<% String[] names = {"CRISPY BRAISED SQUAB","SOY SAUCE SQUAB","PAN FRIED MINCED SQUAB WITH LETTUCE"};
String[] SKUs = {"Squab marinated in five spice seasoning and roasted to prefection.","Whole squab braised with supreme soy sauce.","Stir fried minced squab with ground water chestnuts, Chinese sausage, Chinese mushrooms and served with fresh lettuce cups and hoi sin sauce." };
String[] Cnames ={"N F"," o F","F]"};
double[] prices = {12.00, 12.00, 12.00};
String[] numbername ={"1","1","1"};
String numval[] = new String[3];
for(int j=0; j<names.length; j++){ %>
<TR><TD BGCOLOR=666699 ALIGN=CENTER><b><%=j+1%>.</b></TD>
<TD BGCOLOR=AAAAAA ALIGN=CENTER> <font class="name"><%=names[j]%></font> </TD>
<TD BGCOLOR=AAAAAA ALIGN=CENTER width="9%"><font class="cname"><%=Cnames[j]%></font></TD>
<TD BGCOLOR=AAAAAA ALIGN=CENTER><font class="description"><%=SKUs[j]%></font></TD>
<TD BGCOLOR=AAAAAA ALIGN=CENTER>Squab F</TD>
<TD BGCOLOR=AAAAAA ALIGN=CENTER><b>$<%=prices[j]%>0</b></TD><!--value=shoppingCart.setQty(Integer.parseInt(numval[j]))%>-->
<TD BGCOLOR=AAAAAA ALIGN=CENTER><input type="text" name=<%=numbername[j]%> size="2" > </TD>
<TD BGCOLOR=cccc66>
<A HREF="squab1.jsp?name=<%=names[j]%>&cname=<%=Cnames[j]%>&sku=<%=SKUs[j]%>&price=<%=prices[j]%>&numname=<%=numbername[j]%>">
Add</A></TD></TR>
<% } %>
<TR><TD BGCOLOR=666699 ALIGN=CENTER> </TD>
<TD BGCOLOR=666699 ALIGN=CENTER colspan="2"> </TD>
<TD BGCOLOR=666699 ALIGN=CENTER> </TD>
<TD BGCOLOR=666699 ALIGN=CENTER> </TD>
<TD BGCOLOR=666699 ALIGN=CENTER> </TD>
<TD BGCOLOR=666699 ALIGN=CENTER> </TD>
<TD BGCOLOR=666699 ALIGN=CENTER> </TD>
</TR>
</TABLE>
<b> <p align="center"><input type="submit" name="submit" value="View Orders"></p></b>
<%
String name = request.getParameter("name");
String cname = request.getParameter("cname");
if(name!=null){
if(name.equals("emptyCart")){
shoppingCart.emptyCart();
} else {
String sku = request.getParameter("sku");
double price = Double.parseDouble(request.getParameter("price"));
int qty = Integer.parseInt(request.getParameter( "numname"));
shoppingCart.setQty(qty);
}
ShoppingCarts1 newItem = new ShoppingCarts1(sku, name,cname, "Squab", price);
shoppingCart.addItem(newItem);
}} %>
</FORM></BODY></HTML>
Here is the code that I am using. The only problem is that when I entered the quantity of the order , it did not take my entered number instead the static integer I assigned earlier; String[] numbername ={"1","1","1"}; located in the middle of this code. See I have to assign all quantity to 1 to all the products initially. But If some one want more than one, that person has to type the number on the text box. How can I retrieve that number instead of one. I generate all the numbers inside of the for loop. Please take a closer look at the middle part of the assigned code. Very urgent, thanks ahead.
This is the code that is your problem. You are using an anchor tag to do the actuall submitting of your parameters but you have them hardcoded so numname is always 1. You are not making any kind of reference to the form elements.
> br><A
> HREF="squab1.jsp?name=<%=names[j]%>&cname=<%=Cnames[j]%
> &sku=<%=SKUs[j]%>&price=<%=prices[j]%>&numname=<%=numbe
> name[j]%>">
> Add</A>
bobd3 at 2007-6-29 10:01:07 >

Then how do I enter and pass the values cause this is a for loop. I assign the value by this loop.?thanks
instead of the jsp variable use the document.form.fieldname
Dipanc at 2007-6-29 10:01:07 >
