I'm a beginner in the world of ajax. I have a specific requirement where upon clicking a checkbox, I want my select combo box field to be filled dynamically through ajax. Essentially, when I check the box, I want ajax to retrieve data from the 'two.jsp' file and populate the select box in 'one.jsp' with that data.
Here's the code in one.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script>
function go_here(){
if(document.getElementById('c1').checked){
var xRequest1;
//if(string1=="")
//{
//document.getElementById("Offer_id").innerHTML="";
//return;
}
if(window.XMLHttpRequest)
{
xRequest1=new XMLHttpRequest();
}
else
{
xRequest1=new ActiveXObject("Microsoft.XMLHTTP");
}
xRequest1.onreadystatechange=function ()
{
if((xRequest1.readyState==4) && (xRequest1.status==200))
{
document.getElementById("s1").innerHTML=xRequest1.responseText;
}
}
xRequest1.open("get","two.jsp","true");
xRequest1.send();
}
else{
}
}
</script>
</head>
<body>
<input type="checkbox" id="c1" onclick="go_here();"><br>
<select name="Offer_id" id='s1' >
</select>
</body>
</html>
Now, let's take a look at two.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<option value="5234">abc1</option>
<option value="5235">abc2</option>
<option value="4947">abc2</option>
<option value="5210">abc2</option>
<option value="5208">abc2</option>
<option value="5209">abc2</option>
<option value="3974">abc100</option>
</body>
</html>
Unfortunately, I am encountering issues with populating the options in my select box. Can someone please point me in the right direction?