<%@ page contentType="text/html;charset=windows-1252"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<title>Address Form</title>
<!-- include the AJAX JS File -->
<script src="js/ajax/prototype.js" type="text/javascript"></script>
<SCRIPT LANGUAGE="JavaScript">
/*
onChange event of the dropDownList will calls this function
which has AJAX call to Struts Action class
@param: dropDownList object (this)
@param: URL or Struts Action
*/
function depedentDropDown(obj, url) {
var stateValue = $F(obj)
var pars = "state=" + stateValue;
//alert(stateValue);
var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars,
onComplete: showResponse});
}
/*
Upon completing the request the AJAX will call this method
which is responsible for loading the depedent list from the XML
*/
function showResponse(originalRequest)
{
var list = $('city');
var xmlString = originalRequest.responseXML;
var items = xmlString.getElementsByTagName('labelValueBean');
clearList(list);
if (items.length > 0)
{
for (var i = 0; i < items.length; i++)
{
var node = items[i];
var value = "";
var label = "";
if (node.getElementsByTagName("label")[0].firstChild.nodeValue) {
value = node.getElementsByTagName("label")[0].firstChild.nodeValue;
label = node.getElementsByTagName("value")[0].firstChild.nodeValue;
}
addElementToList(list, value, label);
}
}
else
{
addElementToList(list, "", "-- Select is Empty --");
}
}
/**
remove the content of te list
*/
function clearList(list)
{
while (list.length > 0)
{
list.remove(0);
}
}
/**
Add a new element to a selection list
*/
function addElementToList(list, value, label)
{
var option = document.createElement("option");
option.value = value;
var labelNode = document.createTextNode(label);
option.appendChild(labelNode);
list.appendChild(option);
}
</SCRIPT>
</head>
<body>
<form method="POST" action="">
<fieldset style="padding: 2">
<legend>Address Form</legend>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="58%">
<tr>
<td width="24%">Address</td>
<td width="76%">
<input type="text" name="address1" size="20"/>
</td>
</tr>
<tr>
<td width="24%">Apt#</td>
<td width="76%">
<input type="text" name="address2" size="20"/>
</td>
</tr>
<tr>
<td width="24%">State</td>
<td width="76%">
<select size="1" name="state" onChange="depedentDropDown(this,'ajaxOptionResult.jsp')">
<option>Select State</option>
<option value="VA">VA</option>
<option value="MD">MD</option>
</select>
</td>
</tr>
<tr>
<td width="24%">City</td>
<td width="76%">
<select size="1" name="city">
<option>Select City</option>
</select>
</td>
</tr>
<tr>
<td width="24%">Zip</td>
<td width="76%">
<input type="text" name="zip" size="20"/>
</td>
</tr>
</table>
</fieldset>
<p>
<input type="submit" value="Submit" name="B1"/>
<input type="reset" value="Reset" name="B2"/>
</p>
</form>
</body>
</html>