GeeCON Prague 2014*
The moose likes Servlets and the fly likes show and insert data from list depending on choice of radio button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "show and insert data from list depending on choice of radio button" Watch "show and insert data from list depending on choice of radio button" New topic
Author

show and insert data from list depending on choice of radio button

Kameswari Jyosyula
Ranch Hand

Joined: Feb 20, 2001
Posts: 39
Hi,
I want to know how i should go abt doing the following.
I want to give the users tow choices say A and B (want to use radiobuttons, but am willing to try anything else).If the user selects A I want to show list1(dropdown box) or if the choice is B i want to show list2.Also later when the user submits the form i want to insert A(or B) and the selected choice from the dropdown box into the database.How should i program this?
Also , is it possible to get data into dropdown box from the database directly(through a query)rather than by hardcoding the choices?
Please advice.
TIA
Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
When you come into the page, you could load some javascript arrays with all the selections for A, and another array to hold the selections for B. Then when the user chooses A or B, use javascript to change the values in the drop down menu.
Or you can refresh the page and send the page on the URL line, the selection of A or B. So when the user changes his selection, refresh the page with a parameter showing which was selected by appending it to the URL line, then get the selection from the request and based on that choose which drop down box you would like to display.
And yes, you can choose the drop down menu items from the database. I use a custom tag to do that, but you could also use a bean to read from the database and use JSP to go through th resultset and build the drop down box, or something similar.
HTH
Brian
Kameswari Jyosyula
Ranch Hand

Joined: Feb 20, 2001
Posts: 39
Hi,
I am able to acheive this by using Javascript but that needed hardcoding of the data in the table.I would like to avoid the harcoding.How can i select the data in to the dropdown box?I am doing something like below:
<select name=select1 ....onChange=myfunction()..>
The 'myfunction()' checks for the value of select1 and if it is A displays the various options under A and if its b display for B.
I have created a bean which does select the required data(the method returns all the data of column 1 in my table)How do i pass this data into 'myfunction()'? I tried passing it into a variable like var i = <% =mybean.mymethod() %> but this doesn't work.Can you give me an example of passing the data into the drop down menu, so i can see where i am going wrong.
Thank you for ur help .
Originally posted by Brian Nice:
When you come into the page, you could load some javascript arrays with all the selections for A, and another array to hold the selections for B. Then when the user chooses A or B, use javascript to change the values in the drop down menu.
Or you can refresh the page and send the page on the URL line, the selection of A or B. So when the user changes his selection, refresh the page with a parameter showing which was selected by appending it to the URL line, then get the selection from the request and based on that choose which drop down box you would like to display.
And yes, you can choose the drop down menu items from the database. I use a custom tag to do that, but you could also use a bean to read from the database and use JSP to go through th resultset and build the drop down box, or something similar.
HTH
Brian

DAYANAND BURAMSHETTY
Ranch Hand

Joined: Aug 06, 2001
Posts: 34
Jsp........File
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<script >
function activation(){
for(var i=0;i<document.form1.A.length;i++){
if(document.form1.A[i].checked){
var a=document.form1.A[i].value;
a="/temp/test1.jsp?A="+a;
alert(a);
location.href=a;
}
}
}
</script>
<%
String re= request.getParameter("A");
%>
<form action="/Example" name="form1">
<%
if(re!=null){
if(re.equals("A")){
%>
<select name="as">
<option value="ab">ab</option>
<option value="ac">ac</option>
</select>
<%
}else{
%>
<select name="as">
<option value="bb">bb</option>
<option value="bc">bc</option>
</select>
<%
}
}%>
<body>
<input type=radio name="A" value=A <% if (re!=null) if(re.equals("A")){%> checked <%}%> onclick="activation();">A
<input type=radio name="A" value=B <% if (re!=null) if((re.equals("B"))){%> checked <%}%> onclick="activation();">B
</form>
</body>
</html>

Example ------Servlet----File

public class Example extends HttpServlet{
private HttpSession session = null;
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void forward(HttpServletRequest req,HttpServletResponse res,String url)
throws ServletException, IOException{
res.setContentType("text/html");
RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
rd.forward(req, res);
}

public void service(HttpServletRequest req, HttpServletResponse res)throws IOException,ServletException
{
//(options)select ..name " as " declared in jsp.

String listType=(String)req.getParameter("as");



}//service
}

Dil se....,<BR>Dayanand<BR>0065-8839071(off)<BR>0065-7547034(Res0
Kameswari Jyosyula
Ranch Hand

Joined: Feb 20, 2001
Posts: 39
But in ur code after the if loop u r hardcoding the options for select.I don't want to do this.There instead of hardcoding, I would like to select from the database(by using a bean I am getting the values i need through different methods.How to add the data to the list box by calling those methods)
Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
in your bean, if you have the values stored in some sort of array or collection, either use a getter method to return your collection item and loop through the collection to form your drop down, or use an indexed getter to get particular items from your collection.
Example:
<% String [] AValues = bean.getValues();
if (Avalues != null) { %>
<select name="dropdown">
<% for (int i=0; i<Avalues.length; i++) {
String val = AValues[i]; %>
<option value="<%=val%>"><%=val%></option>
<% } %>
</select>
<% } %>
HTH
Brian
Kameswari Jyosyula
Ranch Hand

Joined: Feb 20, 2001
Posts: 39
Could you plz tell me how to get the data into an array in my bean.I am doing the following and obviously the methos returns only the last record .How to change the following code to get the entire result set/data?Plz help

TIA
Originally posted by Brian Nice:
in your bean, if you have the values stored in some sort of array or collection, either use a getter method to return your collection item and loop through the collection to form your drop down, or use an indexed getter to get particular items from your collection.
Example:
<% String [] AValues = bean.getValues();
if (Avalues != null) { %>
<select name="dropdown">
<% for (int i=0; i<Avalues.length; i++) {
String val = AValues[i]; %>
<option value="<%=val%>"><%=val%></option>
<% } %>
</select>
<% } %>
HTH
Brian

Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
Try something like the following. If you don't know how many itmes can be stored in the array, then you may have to use something like an ArrayList and then convert it to an array using toArray()


Brian
Kameswari Jyosyula
Ranch Hand

Joined: Feb 20, 2001
Posts: 39
Hi I am able to get the data into my checkbox by using Vectors.
But I am not able to change the 2ndcheckbox values based on first one.How should i do that?what needs to be changed in my code?Plz help - under time constraint and only this problem needs to be solved.Would very much appreciate ur help.
here is my code:

The problem is that the reuest.getParameter("x") is not getting anything.The next line(out.print(re) ) give a null value.

I very much appreciate ur help.
TIA

[This message has been edited by Kameswari Jyosyula (edited August 15, 2001).]
Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
On your first drop down, put the code:
<select name="dropdown1" size="1" onchange="reloadPage()">
If your page is called firstPage.jsp, javascript can be something like:
<script language="JavaScript">
function reloadPage() reloadpage='firstPage.jspdropdownValue='+document.form.dropdown.value;
self.location=reloadpage;
}
</script>
in .jsp, use
String value = request.getParameter("dropdownValue");
now use that value to decide how to populate your second drop down menu.
Brian
Kameswari Jyosyula
Ranch Hand

Joined: Feb 20, 2001
Posts: 39
Thank you all for your help , i solved this problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: show and insert data from list depending on choice of radio button