2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

Reply

Join Date: Dec 2004
Posts: 4,273
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 494
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

 
-1
  #11
Jun 28th, 2009
Once you decide to follow MVC standard, you can contact me.
Secondly you should start learning about basic deployment with Tomcat
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 7
Reputation: sriups is an unknown quantity at this point 
Solved Threads: 0
sriups sriups is offline Offline
Newbie Poster

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

 
0
  #12
Jun 28th, 2009
Once you decide to follow MVC standard, you can contact me.
Secondly you should start learning about basic deployment with Tomcat
of course i follow mvc standard i'm using jbdc inside jsp here just for testing purpose i'm going to convert it to MVC standard later.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,273
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 494
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

 
-1
  #13
Jun 28th, 2009
Originally Posted by sriups View Post
of course i follow mvc standard i'm using jbdc inside jsp here just for testing purpose i'm going to convert it to MVC standard later.
You should do it from beginning, that way you wouldn't be pulling double shift and same goes for us
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 22
Reputation: CoSIS1 is an unknown quantity at this point 
Solved Threads: 1
CoSIS1 CoSIS1 is offline Offline
Newbie Poster

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

 
0
  #14
Jun 29th, 2009
the best answer is ajax and actually you do not to learn ajax just copy
the code i will gave and follow the guidlines copy and paste the attached file into your project folder :

1- copy and paste the attached file into your project folder


2- copy the following code and paste into the head of you jsp file

<script type="text/javascript" src="prototype.js"></script>
<script>
function item_code1(value){

var myAjax1=new Ajax.Request('itemcode1.jsp',{
method: 'post',postBody:'id='+value, onSuccess: function(transport) {
var itemcode1=$('itemcode1');
itemcode1.update(transport.responseText);
}
});
}

3- in your itemname combobox html make it exactly like follow

<select name="itemname1" size="1" id="first1" onchange="item_code1($('first1').value);">

4- copy and paste the following code where do you want the itemcode to be appeare

<div id="itemcode1"> </div>
5- now you just need to open new jsp file and copy write the following

  1. <%@ page import="java.util.*" %>
  2. <%@ page import="java.sql.*" %>
  3.  
  4.  
  5. <%
  6. String id=request.getParameter("id");
  7. Vector v1,v2;
  8.  
  9. [COLOR="Red"]jmm[/COLOR].Database database = new jmm.Database(pageContext.getServletContext().getRealPath("/WEB-INF/config.txt")); // depend on yours
  10. database.jdbcConnect();
  11. String sql;
  12.  
  13. sql = "select itemcode from item where itemname='"+id+"'";
  14.  
  15.  
  16.  
  17. out.print("<select name=itemcode1 id=\"itemcode \">");
  18.  
  19. v1=database.jdbcMultipleRowQuery(sql);
  20. if(v1.size()!=0){
  21. for(int i=0;i<v1.size();i++){
  22. v2 = (Vector)v1.elementAt(i);
  23. for(int j=0;j<v2.size();j++){
  24. out.print("<option>"+v2.get(0)+"</option>");
  25. }
  26. }
  27. out.print("</select>");
  28.  
  29. }else{
  30.  
  31. }
  32.  
  33.  
  34.  
  35.  
  36. database.jdbcConClose();
  37. database = null;
  38.  
  39. %>


5- i wish it will help ,
Last edited by peter_budo; Jun 29th, 2009 at 4:05 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Attached Files
File Type: js prototype.js (97.3 KB, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 22
Reputation: CoSIS1 is an unknown quantity at this point 
Solved Threads: 1
CoSIS1 CoSIS1 is offline Offline
Newbie Poster

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

 
0
  #15
Jun 29th, 2009
just inform me if it does not work
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

 
0
  #16
Jun 29th, 2009
CoSIS1>the best answer is ajax and actually you do not to learn ajax just copy the code i will gave and follow the guidlines copy and paste the attached file into your project folder.

How did you know AJAX? just copy the code?

Wrap up source code with bb code tags.
Read
How to use BB code tags?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 22
Reputation: CoSIS1 is an unknown quantity at this point 
Solved Threads: 1
CoSIS1 CoSIS1 is offline Offline
Newbie Poster

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

 
0
  #17
Jun 29th, 2009
am new member here, and i just wanted to help here
plus this code is working , i have used it before .
actually i know some in ajax that helped me before in my projects
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 21
Reputation: amarjeetsingh has a little shameless behaviour in the past 
Solved Threads: 1
amarjeetsingh amarjeetsingh is offline Offline
Newbie Poster

Re: 2 comboboxes values of 2nd depends on 1st ,OPTION values from Database

 
0
  #18
Jun 29th, 2009
  1. <html>
  2. <head><title>Sales Page/title></head>
  3. <body>
  4. <script type="text/javascript">
  5. function ajaxFunction()
  6. {
  7. var xmlHttp;
  8. try
  9. { // Firefox, Opera 8.0+, Safari
  10. xmlHttp=new XMLHttpRequest();
  11. }
  12. catch (e)
  13. { // Internet Explorer
  14. try
  15. {
  16. xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  17. }
  18. catch (e)
  19. {
  20. try
  21. {
  22. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  23. }
  24. catch (e)
  25. {
  26. alert("Your browser does not support AJAX!");
  27. return false;
  28. }
  29. }
  30. }
  31.  
  32. xmlHttp.onreadystatechange=function()
  33. {
  34. //alert("i m not in ready state");
  35.  
  36. if(xmlHttp.readyState==0)
  37. {
  38. //document.myForm.time.value=xmlHttp.responseText;
  39. alert("The request is not initialized");
  40. }
  41. if(xmlHttp.readyState==1)
  42. {
  43. //document.myForm.time.value=xmlHttp.responseText;
  44. alert("The request has been set up");
  45. }
  46. if(xmlHttp.readyState==2)
  47. {
  48. //document.myForm.time.value=xmlHttp.responseText;
  49. alert("The request has been sent");
  50. }
  51. if(xmlHttp.readyState==3)
  52. {
  53. //document.myForm.time.value=xmlHttp.responseText;
  54. alert("The request is in process");
  55. }
  56. if(xmlHttp.readyState==4)
  57. {
  58. //document.myForm.time.value=xmlHttp.responseText;
  59. //alert("i m in ready state");
  60. document.getElementById("theResponse").innerHTML = xmlHttp.responseText;
  61. }
  62. }
  63.  
  64.  
  65. var itemname=document.myForm.itemname.value;
  66. var url="getProduct.jsp";
  67. url=url+"?itemname="+itemname+"&sid="+Math.random(); //url=url+"&sid="+Math.random();
  68.  
  69. xmlHttp.open("GET",url,true);
  70. xmlHttp.send(null);
  71. }
  72. </script>
  73. select itemname:
  74. <form name="myForm">
  75. /*
  76.   wrong code------
  77.   <option>processors</option>
  78. <option>speakers</option>
  79. <option>monitor</option>
  80. <option>mouse</option>
  81.  
  82. due to this code you can not get
  83. the value in the request.getParameter("itemname");
  84.  
  85. */
  86.  
  87. <!-- right code --->
  88.  
  89. <select name="itemname" onChange="ajaxFunction();">
  90. <option value="processors">processors</option>
  91. <option value="speakers">speakers</option>
  92. <option value="monitor">monitor</option>
  93. <option value="mouse">mouse</option>
  94.  
  95.  
  96.  
  97.  
  98.  
  99. </select>
  100. <div id="theResponse">
  101.  
  102. </div>
  103. </form></body>
  104. </html>

Now your second page getproduct.jsp

i have removed un necessary code

  1.  
  2. <%@ page language="java" contentType="text/html" import="java.sql.*" %>
  3.  
  4. <html>
  5. <head><title>select box demos</title></head>
  6. <body>
  7.  
  8. <%
  9. try
  10. {
  11. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  12. Connection conn=DriverManager.getConnection("jdbc:odbc:select");
  13. try
  14. {
  15.  
  16. String val=request.getParameter("itemname");
  17.  
  18. //String val1="speakers";
  19. //String val2="processors";
  20.  
  21.  
  22. // String itname[]={};
  23.  
  24. // int i=0,j=0,m=0;
  25.  
  26. String str="select * from stocks where item_name=?";
  27.  
  28. PreparedStatement stmt=conn.prepareStatement(str);
  29.  
  30. stmt.setString(1,val);
  31.  
  32. ResultSet rs=stmt.executeQuery();
  33.  
  34. /* itname=new String[100];
  35.  
  36.   while(rs.next())
  37.   {
  38.   itname[m]=rs.getString(3);
  39.   m++;
  40.   }
  41.  
  42.  
  43.   for(j=m;j<100;j++)
  44.   {
  45.   itname[j]="null";
  46.   } */
  47.  
  48. %>
  49.  
  50. Prod Name: <select name="productname">
  51. <%
  52. while(rs.next())
  53. {
  54. // if(itname[i]!="null")
  55. out.println("<option value="+rs.getString(3)+">"+rs.getString(3)+"</option>");
  56. //i++;
  57. }
  58. // out.println("</select>");
  59.  
  60. }
  61. catch(SQLException e)
  62. {
  63. out.print("SQL Not Executing");
  64. }
  65. }
  66. catch(Exception e)
  67. {
  68. e.printStackTrace();
  69. }
  70.  
  71. finally
  72. {
  73.  
  74.  
  75. try
  76. {
  77. if(rs!=null)
  78. {
  79. rs.close();
  80. rs=null;
  81.  
  82. }
  83. if(stmt!=null)
  84. {
  85. stmt.close();
  86. stmt=null;
  87.  
  88. }
  89.  
  90. if(conn!=null)
  91. {
  92. conn.close();
  93. conn=null;
  94.  
  95. }
  96.  
  97.  
  98. }catch(SQLException e)
  99. {
  100. out.print("SQL Not Executing"+e);
  101. }
  102.  
  103.  
  104.  
  105.  
  106. }
  107.  
  108.  
  109.  
  110. %>
  111. </select>
  112.  
  113. </body>
  114. </html>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2044 | Replies: 17
Thread Tools Search this Thread



Tag cloud for JSP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC