JAVASCRIPTINGTaking advantage of the elements array
JAVASCRIPTING
Taking advantage of the elements array
Steven W. Disbrow
Listing 2. Encoding information in HTML names.
<HTML>
<HEAD>
<TITLE>Using the "elements" Array</TITLE>
<script language="javascript">
function makeUpdateSQL( theForm) {
var resultSQL = "";
// Look at each element in the form
for (x in theForm.elements) {
// Weed out elements that we are not interested in
if (x.indexOf("$") != -1) {
// break apart the employee ID, db field and data type info
ed = x.split('$');
// determine table and field name
df = ed[1].split('.');
// create the SQL to update this field
resultSQL += "update " + df[0] + " set " + df[1] + "=";
if (ed[2] == "num")
resultSQL += theForm.elements[x].value +
" where employeeID=" + ed[0] + "\n";
else
resultSQL += "'"+theForm.elements[x].value +
"where employeeID="+ed[0]+ "\n";
}
}
theForm.outputSQL.value = resultSQL;
}
</script>
</HEAD>
<BODY>
<form name="form1">
Make your changes and click the Update button:
<table border="1">
<tr>
<th>First Name</th><th>Last Name</th><th>Salary</th>
</tr>
<tr>
<td>
<input type="text" name="1$employees.fname$char" value="Fred">
</td>
<td>
<input type="text" name="1$employees.lname$char"value="Smith">
</td>
<td>
<input type="text" name="1$salary.amount$num" value="20000">
</td>
</tr>
<tr>
<td>
<input type="text" name= "2$employees.fname$char"value="John">
</td>
<td>
<input type="text" name="2$employees.lname$char" value="Doe">
</td>
<td>
<input type="text" name="2$salary.amount$num" value="15000">
</td>
</tr>
<tr>
<td>
<input type="text" name="3$employees.fname$char" value="Jane">
</td>
<td>
<input type="text" name="3$employees.lname$char" value="Doe">
</td>
<td>
<input type="text" name="3$salary.amount$num" value="25000">
</td>
</tr>
</table>¡
<input type="button" value="Update"
onClick="makeUpdateSQL( this.form)"><br>
<textarea name="outputSQL" rows="15" cols="80"></textarea>
</form>
</BODY>
</HTML>
About the Author
Steven Disbrow is the owner of EGO Systems, a computer consulting firm in Chattanooga, TN. He can be contacted at [email protected].