I'm looking to make a table add/remove rows on button click (javascript) and then submit the data input into a mysql database (php).
The table so far
<html>
<head>
<title>Nomad JMDB - Create</title>
<script type="text/javascript">
function AddRow ()
{
var o_txt0 = document.createTextNode (document.getElementById ("01Bno").value);
var o_txt1 = document.createTextNode (document.getElementById ("01Type").value);
var o_txt2 = document.createTextNode (document.getElementById ("01Class").value);
var o_button = document.createElement ("input");
o_button.type = "button";
o_button.value = "Remove";
o_button.onclick = RemoveRow;
var o_td0 = document.createElement ("td");
var o_td1 = document.createElement ("td");
var o_td2 = document.createElement ("td");
var o_td3 = document.createElement ("td");
var o_tr = document.createElement ("tr");
var o_body = document.getElementById ("dynamic_table_body");
o_td0.appendChild (o_txt0);
o_td1.appendChild (o_txt1);
o_td2.appendChild (o_txt2);
o_td3.appendChild (o_button);
o_tr.appendChild (o_td0);
o_tr.appendChild (o_td1);
o_tr.appendChild (o_td2);
o_tr.appendChild (o_td3);
// document.form1.01Bno.value = document.form1.01Bno.value + 1;
o_body.appendChild (o_tr);
}
function RemoveRow ()
{
var dinosaur = this.parentNode.parentNode;
dinosaur.parentNode.removeChild (dinosaur); // :-)
}
</script>
</head>
<body style="font-family: Arial">
<form id="form1" name="form1" method="post" action="create2.php">
</p>
<br>
<table border="1" cellpadding="0" cellspacing="0">
<tbody id="dynamic_table_body"><tr>
<td width="92" height="102">Building #</td>
<td width="63">Type</td>
<td width="81">Class</td>
<td width="221">
<table width="200" cellpadding="0">
<tr>
<td>#: </td>
<td><input name="01Bno" type="text" id="01Bno" value="001" size="10"></td>
</tr>
<tr>
<td>Type: </td>
<td><select name="01Type" id="01Type">
<option value="Type 1" selected>Type 1</option>
<option value="Type 2">Type 2</option>
</select></td>
</tr>
<tr>
<td>Class:</td>
<td><select name="01Class" id="01Class">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select></td>
</tr>
<tr>
<td colspan="2"><button type="button" onClick="AddRow ();">Add Building</button></td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
<p>
<label>
<input type="reset" name="clear" id="clear" value="Clear" />
<input type="submit" name="create" id="create" value="Submit" />
</label>
</p>
</form>
</body>
</html>
I don't know how to then use the data in the table when submitting the form. I would prefer a dynamic table, hence not php created (and no page refresh required). Any ideas?
I'm also stuck on incrementing the "01Bno" textbox by one for each new row, (If possible, it should be padded - eg 001, 002, ...)
Something like the following?
document.form1.01Bno.value = document.form1.01Bno.value + 1;
Thanks in advance for any help
