alright i have a HTML form that must use POST to send data to a servlet.
I need 4 pieces of information the year and month (which i have no problem
getting) and a list of meter ids (integers) and readings (integers).
The list of meter ids and readings are received by the servlet as null pointers.
This is obviously a problem and I need them as two separate strings.
Example wanted result:
System.out.prinln("meter_ids = " + meter_ids);
120,121,122,123,124
and
System.out.prinln("readings = " + readings);
4120,1261,1222,1523,1248
The size of the list of meters is dynamic so the number of input boxes will change.
So i name the boxes meter0, meter1, meter2… and reading0, reading1,…
So, my idea was to encapsulate all of the input boxes into a single string then use
POST to send the data to the servlet by using a JavaScript function.
Below is my attempt at the HTML form code and the JavaScript.
The form is as follows:
<form action="servlet" id="saveform" name="saveform" method="POST">
<!-- SAVE button -->
<input type="button" align="right" value="SAVE" onclick="doSave();" >
<!-- table to input readings -->
<table class="readings">
<tr>
<!-- get year -->
<td class="td">
<label align="left"> Year: </label>
<select name="year">
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
</select>
</td>
<!-- get month -->
<td class="td">
<label align="left"> Month: </label>
<select name="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
</select>
</td>
</tr>
<tr class="header">
<td class="td"> Meter </td>
<td class="td"> Reading (gallons) </td>
</tr>
<%
for (int i = 0; i < meters.size(); ++i) {
cur_meter = (WaterMeter)meters.get(i);
/* get the data */
group = cur_meter.getGroup();
name = cur_meter.getName();
meter_id = cur_meter.getMeterID();
%>
<input value="<%= meter_id %>" name="meter<%= i %>" type="hidden" id="meter<%= i %>"></input>
<%
if ( !group.contentEquals(tmp) ) {
%>
<tr>
<td align="left" class="tenant_cells"> <%= group %> </td>
<td align="center" class="tenant_cells"></td>
</tr>
<%
} /* end if */
%>
<tr>
<td align="left" class="meter_cells"> <%= name %> </td>
<td align="center" class="td">
<input name="reading<%= i %>" id="reading<%= i %>" class="flat" />
</td>
</tr>
<%
tmp = group;
} /* end for() */
%>
</table>
</form>
JAVASCRIPT is a follows:
<script type="text/javascript">
// arrays to be sent to servlet
var meter_ids = new Array();
var readings = new Array();
function doSave() {
// get the data from the page
<c:set var="num" value="${0}" />
<c:forEach var ="block" items="${DisplayData.meterList}" >
document.getElementById("meter<c:out value="${num}" />").value = meter_ids.join('^');
document.getElementById("reading<c:out value="${num}" />").value = readings.join('^');
<c:set var="num" value="${num + 1}" />
</c:forEach>
document.saveform.submit();
}
</script>
So my question is: How do I send the JavaScript vars in question (meter_ids, and
readings) to my servlet properly?