By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,638 Members | 2,204 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,638 IT Pros & Developers. It's quick & easy.

want data base connection in focus lost of a textbox in the table

P: 1
i wrote a code in jsp to create dynamic table..the problem is i need data base connection when cursor moves from one cell to other...
eg...when i enter product id in the first cell, the product name and its rate should come in the next two cells...it should take from the data base...

it's my code........

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  2. "http://www.w3.org/TR/html4/loose.dtd" > 
  3. <%@ page import="java.sql.*" %> 
  4. <%@ page import="java.io.*" %> 
  5.  
  6. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  7.  
  8. <head>
  9.   <title></title>
  10. <script language="JavaScript" type="text/javascript">
  11. /*<![CDATA[*/
  12.  
  13.     var i=1,j=0,k=0,tot=0,vat=0,totrow=0;
  14. function Add(id){
  15.  var id1='ta'+i;
  16.  var id2='tb'+i;
  17.  var id3='tc'+i;
  18.  var id4='td'+i;
  19.  var id5='te'+i;
  20.  var table=document.getElementById(id);
  21.  var tbody=table.getElementsByTagName('TBODY')[0];
  22.  var newrow=tbody.appendChild(table.rows[0].cloneNode(true));
  23. // newrow.getElementsByTagName('SELECT')[0].selectedIndex=0; for combobox
  24.  newrow.getElementsByTagName('INPUT')[0].value='';
  25.  newrow.getElementsByTagName('INPUT')[0].id=id1;
  26.  newrow.getElementsByTagName('INPUT')[0].name=id1;
  27.  
  28.  newrow.getElementsByTagName('INPUT')[1].value='';
  29.  newrow.getElementsByTagName('INPUT')[1].id=id2;
  30.  newrow.getElementsByTagName('INPUT')[1].name=id2;
  31.  
  32.  newrow.getElementsByTagName('INPUT')[2].value='';
  33.  newrow.getElementsByTagName('INPUT')[2].id=id3;
  34.  newrow.getElementsByTagName('INPUT')[2].name=id3;
  35.  
  36.  newrow.getElementsByTagName('INPUT')[3].value='';
  37.  newrow.getElementsByTagName('INPUT')[3].id=id4;
  38.  newrow.getElementsByTagName('INPUT')[3].name=id4;
  39.  
  40.  newrow.getElementsByTagName('INPUT')[4].value='';
  41.  newrow.getElementsByTagName('INPUT')[4].id=id5;
  42.  newrow.getElementsByTagName('INPUT')[4].name=id5;
  43.  
  44.  newrow.getElementsByTagName('INPUT')[5].checked=false;
  45.  i++;
  46. }
  47.  
  48. function Remove(id,nme){
  49.  var table=document.getElementById(id);
  50.  var rows=table.rows;
  51.  for (var remove=[],zxc0=0;zxc0<rows.length;zxc0++){
  52.   var cb=rows[zxc0].getElementsByTagName('INPUT')[5];
  53.   if (cb&&cb.name==nme&&cb.checked){
  54.    remove.push(rows[zxc0]);
  55.   }
  56.  }
  57.  if (remove.length==rows.length) return alert('U cant remove all rows');
  58.  for (var zxc1=0;zxc1<remove.length;zxc1++){
  59.   remove[zxc1].parentNode.removeChild(remove[zxc1]);
  60.  }
  61. }
  62.  
  63. /*
  64. function Remove(id){
  65.  var table=document.getElementById(id);
  66.  var rows=table.rows;
  67.  for (var zxc0=0;zxc0<rows.length;zxc0++){
  68.   if (rows[zxc0].getElementsByTagName('INPUT')[5].checked){
  69.    rows[zxc0].parentNode.removeChild(rows[zxc0]);
  70.    zxc0--;
  71.   }
  72.  }
  73. }
  74. */
  75. function showText(tid,bid)
  76. {
  77.     var txt=document.getElementById(tid);
  78.     var but=document.getElementById(bid);
  79.     txt.type="textbox";
  80.     but.type="button";
  81.     //but.onclick="hideText()";
  82.     alert("Please Enter Purchase.Order.No and click OK");
  83. }
  84. function hideText(tid1,bid1)
  85. {
  86.     var txt1=document.getElementById(tid1);
  87.     var but1=document.getElementById(bid1);
  88.     txt1.type="hidden";
  89.     but1.type="hidden";
  90.  
  91. }
  92. function test(tt0)
  93. {
  94.     var dd='ta'+j;
  95.     var ttxt0=document.getElementById(tt0);
  96.     var ttxt1=document.getElementById(dd);
  97.     var ppnam=ttxt1.value;
  98.     //var ppnam=ttxt1.value;
  99.     ttxt0.value=ppnam;
  100.     j++;
  101.  
  102. }
  103. function test5(tt5)
  104. {
  105.     var dd3='tc'+k;
  106.     var dd4='td'+k;
  107.  
  108.     var ttxt5=document.getElementById(tt5);
  109.     var ttxt3=document.getElementById(dd3);
  110.     var ttxt4=document.getElementById(dd4);
  111.  
  112.     var num2=ttxt4.value;
  113.     var num1=ttxt3.value;
  114.  
  115.     var val=0;
  116.     val=parseFloat(num1) * parseFloat(num2);
  117.     ttxt5.value=val;
  118.     k++;
  119. }
  120. function cal(id)
  121. {
  122.     var table=document.getElementById(id);
  123.     var rows=table.rows;
  124.     for (var zxc0=0;zxc0<rows.length;zxc0++)
  125.      {
  126.         //tot=tot+5;
  127.         tot=parseFloat(tot)+parseFloat((rows[zxc0].getElementsByTagName('INPUT')[4].value));
  128.         totrow++;
  129.      }
  130.      vat=parseFloat(tot/12.5);
  131.      document.product.vate.value=vat;
  132.      document.product.total.value=parseFloat(vat+tot);
  133.      document.product.rcount.value=totrow;
  134.      vat=0;
  135.      tot=0;
  136.      totrow=0;
  137. }
  138. </script>
  139.  
  140. <body>
  141.  
  142. <form name="product" action="mynew.jsp" method="get">
  143. <table id="head" border="1">
  144. <tbody>
  145. <tr><td colspan="50"><b>&nbsp;Product Id&nbsp;&nbsp;&nbsp;</b></td><td colspan="50"><b>&nbsp;Product Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td><td colspan="50"><b>&nbsp;Quantity&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td><td colspan="30"><b>&nbsp;Rate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td><td colspan="30"><b>&nbsp;Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></b></td>
  146.   </tr>
  147. </tbody>
  148. </table>
  149. <table id="tst" border="1">
  150. <tbody>
  151.   <tr>
  152.     <td>
  153.     <input name="ta0" type="textbox" size="10" id="ta0"/>
  154.     <input name="tb0" type="textbox" size="30" id="tb0" onfocus="test(id);"/>
  155.     <input name="tc0" type="textbox" size="10" id="tc0"/>
  156.     <input name="td0" type="textbox" size="10" id="td0"/>
  157.     <input name="te0" type="textbox" size="10" id="te0" onfocus="test5(id);" readonly/>
  158.     <input type="checkbox" name="bb[]" id="cbox"/></td>
  159.   </tr>
  160. </tbody>
  161. </table>
  162. <input type="button" name="" value="Add" onclick="Add('tst');"/>
  163. <input type="button" name="" value="Remove" onclick="Remove('tst','bb[]');"/>
  164. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  165. <input name="txt1" type="hidden" id="textid" size="10"/>
  166.  
  167. <input name="" value="OK" type="hidden" id="buttonid" size="10" onclick="hideText('textid','buttonid');"/>
  168. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  169. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  170. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  171. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  172. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  173. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  174.  
  175. <input type="button" value="calculate" onclick="cal('tst');"/>
  176. <br>
  177. <br>
  178. Vat:&nbsp;&nbsp;
  179. <input type="textbox" name="vate"/>
  180. <br>
  181. <br>
  182. Total:
  183. <input type="textbox" name="total"/>
  184. <br>
  185. <br>
  186. <input type="button" name="" value="Edit" onclick="showText('textid','buttonid');"/>
  187. <input type="submit" name="" value="Save"/>
  188. <br>
  189. <br>
  190. <input type="textbox" name="rcount"/>
  191. <%
  192.  
  193. %>
  194. <br>
  195.  
  196. <%
  197.  int count=0;
  198.   try { count = Integer.parseInt(request.getParameter("rcount")); 
  199.   }
  200.     catch (Throwable t) { }
  201.  
  202.  String pid[]=new String[count];
  203.  String pnam[]=new String[count];
  204.  String qtyt[]=new String[count];
  205.  String rate[]=new String[count];
  206.  String value[]=new String[count];
  207.  String vatt=null,tot=null;
  208.  
  209. %>
  210.  
  211. <input type="textbox" name="rnum" value=<%=count%>>
  212.  
  213. </form>
  214.  
  215. </body>
  216. </head>
  217. </html>
Sep 22 '08 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
Please use code tags when posting code.
What you need is usually done using AJAX these days.
Sep 23 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.