
Hi
I am using jsp and ajax.I am retrieving the entire content from database(al.jsp) and put it into a textbox.When i am clicking a combo box,it updates the selected value in the database(jag.jsp).
But it only works for first textbox and combobox.I noticed that it requires different id for textbox and combobox. But i am not able to assign unique id.
Here is my code. Plz anybody help me.Its urgent.
al.jsp<%@ page import="java.sql.*" %>
<%@ page import="javax.swing.*" %>
<%! Connection c;
Statement s;
ResultSet rs;
String cid,roll;
int i=0;
int j=1;
%>
<% try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c=DriverManager.getConnection("jdbc:odbc:comdsn1");
s=c.createStatement();
rs=s.executeQuery("select * from comt1");
}
catch(Exception e)
{} %>
<html>
<script>
function ajax(str){
var xmlHttp;
var i;
var config=document.getElementById('comboBoxMenu').value;
var config1=document.getElementById('t1').value;
var tids=document.getElementById('comboBoxMenu').getAttribute('id');
alert("coboid:"+tids);
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlHttp=new XMLHttpRequest();
}
catch(e){
alert("your browser cant support");
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4 && xmlhttp.status==200){
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
document.write(xmlHttp.responseText);
}
}
var url="jag.jsp";
url=url+"?q="+config+"&q1="+config1;
xmlHttp.open("POST",url,true);
xmlHttp.send(url);
}
</script>
<body>
<form method="post" name="jag">
<center><table>
<%
try
{
rs=s.executeQuery("select * from comt1");
while(rs.next())
{
roll=rs.getString("rollno");
i=i+1;
j=j+1;
%>
<tr><td><input type="text" value="<%=roll%>" name="t1" id="${i}" readonly></td><td>
<select name="comboBoxMenu" id="${j}" onchange="ajax(this.value);">
<option value="present">present</option>
<option value="absent">absent</option>
</select>
</td>
<td></tr>
<% }
}
catch(Exception e){}
%>
</table>
</center>
</form>
<div id="myDiv">
</div>
</body>
</html>
jag.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@ page import="java.sql.*" %>
<%@ page import="javax.swing.*" %>
<% String s1=request.getParameter("q");
String s2=request.getParameter("q1");
%>
<%try{
Connection c;
Statement st;
ResultSet rs;
JOptionPane.showMessageDialog(null,"welcome");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c=DriverManager.getConnection("jdbc:odbc:comdsn1");
st=c.createStatement();
rs=st.executeQuery("select * from comt1");
JOptionPane.showMessageDialog(null,"welcome updated"+s2+s1);
st.executeUpdate("update comt1 set [9-10]='"+s1+"' where rollno='"+s2+"'");
rs=st.executeQuery("select * from comt1");
JOptionPane.showMessageDialog(null,"updated");
}
catch(Exception ex){
out.println(ex);
}
%>
Thanks in advance.Anybody help me.its urgent plz.

Hi,I am sakthilakshmi. Try this code.I think its useful for u.
<html>
<script>
function ajax(str){
var xmlHttp;
var config=document.getElementById('comboBoxMenu').value;
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlHttp=new XMLHttpRequest();
}
catch(e){
alert("your browser cant support");
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4 && xmlhttp.status==200){
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
document.write(xmlHttp.responseText);
}
}
var url="jag.jsp";
url=url+"?q="+config;
xmlHttp.open("POST",url,true);
xmlHttp.send(url);
}
</script>
<body>
<form method="post" name="jag">
<select name="comboBoxMenu" onchange="ajax(this.value);">
<option value="present">present</option>
<option value="absent">absent</option>
</select>
</form>
<div id="myDiv">
</div>
</body>
</html>
jag.jsp
<html>
<% String s1=request.getParameter("q");
out.println(s1); //will display the combo box value
%>
</html>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.