Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

issue with returning from a function on a onClick event

 
Ashwin venkatesh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Every one,

I am facing a problen in retrieving data from the database and displaying the same in there respective textboxes in a jsp page on a onClick event. ie i have button Next i call a javascript function in a jsp page with some textfields. when i press the Next button the next record in the database should be displayed. My code is as follows...

<%
ResultSet rs1 = null;
rs1= stmt.executeQuery("select * from custtable");
//rs1.first();

%>


function next_rec(kform)
{
var xx = document.usrreg.next_button.value;
var zz = xx.toString();

if(xx == 'Next')
{

<%
try

{
rs1.next();
int RECNO = rs1.getInt("recordno");
String DCODE = rs1.getString("dcode");
String CUSTCODE = rs1.getString("custcode");
String CUSTNAME1 = rs1.getString("custname");
String ADDR = rs1.getString("addr");
String CITY = rs1.getString("city");
%>

document.usrreg.recordno.value = '<%=rno%>';
document.usrreg.dcode.value = '<%=DCODE%>';
document.usrreg.custcode.value = '<%=CUSTCODE%>';
document.usrreg.custname.value = '<%=CUSTNAME1%>';
document.usrreg.addr.value = '<%=ADDR%>';

document.usrreg.city.value = '<%=CITY%>';

<%

if(rs1.isAfterLast())
rs1.first();


}//try ends
catch(Exception e)
{
System.out.println(e.toString());
}
%>
}
}
<form name="usrreg">

<table>

<tr><td>Record No</td><td <input type="text" name="recordno" readonly value=<%= count%>></td>

<tr><td>Dealer Code</td><td><input type="text" name="dcode" readonly value= <%= delcode%>></td>

<tr><td>Customer Code</td><td><input type="text" name="custcode" readonly value= <%= cust_code%>></td>

<tr><td>Customer Name</td><td><input type="text" name="custname" onchange="return customer_name(this.form,'1')"></td>

<tr><td>Address</td><td><textarea name="addr" rows="5" cols="15"> </textarea>

<tr><td>City</td><td><input type="text" name="city" onchange="return city_name(this.form)"></td>

</table>

<tr><td> <input type="button" value="Next" onklick="next_rec(this.form)" name="next_button"></td>
</tr>
</form>
i have changed to onklick because problem in posting my request

the values are displayed only once when the form is loaded, for another onClick event it is not displaying the values.

pls help me at the earliest..
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to read this article [http://www.javaranch.com/journal/200510/Journal200510.jsp#a1] that bear wrote to understand how JavaScript and JSP code actually works.

Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Link to article.

Not to mention that doing SQL access in a JSP is pretty much universally considered an extremely poor practice.
[ August 08, 2007: Message edited by: Bear Bibeault ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic