I want to insert data from dynamic page to mysql database.
I am having master detail table with foreign key between them.
master table 'bill_of_material' & its column are "bom_id, product_id, product_qty, unit_id "
detail table 'bill_of_material_detail' & its column are "bom_id, item_id, item_qty, unit_id "
I have developed dynamic page which adds rows. following are the code.
<script language="Javascript" type="text/javascript">
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
//alert(newcell.childNodes);
switch(newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
}
function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) {
alert("Cannot delete all the rows.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}catch(e) {
alert(e);
}
}
</SCRIPT>
<?php
$options_item="";
$units_set_item = get_all_items();
while ($row_item=mysql_fetch_array($units_set_item))
{
$value_item = $row_item ["item_id"];
$id_item=$row_item["item_name"];
$thing_item=$row_item["item_code"];
$br ="(";
$br1=")";
$item =$id_item." ".$br.$thing_item.$br1;
$options_item.="<OPTION VALUE=\"$value_item\">".$item.'</option>';
}
?>
<form action="create_item.php" method="post">
<p>
Product :
<SELECT NAME=item_primery_unit>
<OPTION VALUE=0>
<?php echo $options_item;?>
</SELECT>
</p>
<p> Product qty :
<input type="text" name="item_name" value="" />
</p>
<?php
$options="";
$units_set = get_all_units();
while ($row=mysql_fetch_array($units_set))
{
$value = $row ["unit_id"];
$id=$row["unit_name"];
$thing=$row["unit_abbr"];
$br ="(";
$br1=")";
$unit=$id." ".$br.$thing.$br1;
$options.="<OPTION VALUE=\"$value\">".$unit.'</option>';
}
?>
<p>
Product unit :
<SELECT NAME=item_primery_unit>
<OPTION VALUE=0>
<?php echo $options?>
</SELECT>
</p>
<p align= "center">
<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
<!-- <INPUT type="button" value="Save Data" onclick="SaveRow('dataTable')" /> -->
<p/>
<TABLE id="display_header" BORDER="2" CELLPADDING="4" CELLSPACING="2" WIDTH="870">
<tr>
<td WIDTH="70">Row</td>
<td WIDTH="200">Source item</td>
<td WIDTH="200">Source item qty</td>
<td WIDTH="200">source item unit</td>
</tr>
</TABLE>
<TABLE id="dataTable" BORDER="2" CELLPADDING="4" CELLSPACING="2" WIDTH="870">
<TR>
<TD WIDTH="70"><INPUT type="checkbox" name="chk"/></TD>
<td WIDTH="200"><SELECT NAME=item_primery_unit>
<OPTION VALUE=0>
<?php echo $options_item;?>
</SELECT>
</td>
<td WIDTH="200"> <input type="text" name="item_name" value="" /></td>
<td WIDTH="200" ><SELECT NAME=item_primery_unit>
<OPTION VALUE=0>
<?php echo $options?>
</SELECT>
</td>
</TR>
</TABLE>
<input type="Submit" name="submit" value="Save Item" />
</form>