Greetings all...
I have cobbled together a form setup which allows users to dynamically add as many rows as are needed, then submit the completed form for insertion into a MySQL database. I have the form set up more or less as I want it, but there a couple of things that are giving me difficulty.
1) Whenever a new row is added, the values from the first row are inserted into the new row. I want this new row to be blank.
2) Is there a way I can increment the field names? i.e., first row fields are instrument1, us_date1, etc.. Second row fields are instrument2, us_date2, etc..., and so on.
I'm trying my best to understand what this script is doing, but can't seem to get a handle on it...I'm pretty new at working with .js. I suspect a solution to #2 might help solve #1, but I'd like to hear what you all have to say about it.
Thanks for any suggestions. Code is below.
I have cobbled together a form setup which allows users to dynamically add as many rows as are needed, then submit the completed form for insertion into a MySQL database. I have the form set up more or less as I want it, but there a couple of things that are giving me difficulty.
1) Whenever a new row is added, the values from the first row are inserted into the new row. I want this new row to be blank.
2) Is there a way I can increment the field names? i.e., first row fields are instrument1, us_date1, etc.. Second row fields are instrument2, us_date2, etc..., and so on.
I'm trying my best to understand what this script is doing, but can't seem to get a handle on it...I'm pretty new at working with .js. I suspect a solution to #2 might help solve #1, but I'd like to hear what you all have to say about it.
Thanks for any suggestions. Code is below.
Code:
<html>
<head>
<script language="JavaScript" type="text/JavaScript">
function addRow(){
tBody = document.getElementById('myTab').firstChild;
tBody.appendChild(tBody.childNodes[1].cloneNode(true));
theForm = document.forms[0];
theForm.Item.selectedIndex=-1;
fNames = ["instrument", "us_date", "hours", "rate", "charge", "notes"];
for(i=0;i<fNames.length;i++){
theFields = theForm[fNames[i]];
theFields[theFields.length-1].value = theFields[0].defaultValue;
}
}
</script>
</head>
<body>
<form name="FAform" method="get">
<table id=myTab>
<tr>
<th>Instrument</th>
<th>Date</th>
<th>Hours</th>
<th>Rate</th>
<th>Charge</th>
<th>Notes</th>
</tr>
<tr><td>
<select name="instrument">
<option value="Option1">Option 1</option>
<option value="Option2">Option 2</option>
<option value="Option3">Option 3</option>
</select>
</td><td>
<input name="us_date" type=text id="date1" value="" size="10">
</td>
<td><input name=hours type=text id="hours" value="" size="4"></td>
<td><input name=rate type=text id="rate" value="" size="4"></td>
<td><input name=charge type=text id="charge" value="" size="4"></td>
<td><input name=notes type=text id="notes" value="" size="10"></td>
</tr>
</table>
<input type=button value=AddRow onClick="addRow()">
<input type=submit>
<p>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
<input type="button" value="Add Row" onClick="addRow()">
</p></td>
</tr>
<p> </p>
</form>
<p> </p>
</body>
</html>