Hello,

I've got two select boxes and a script that adds a new one each time the user presses "add". The first select box is named "from" and the second "to". After the user makes the selection the program runs a query and returns a result depending on the selection made. The program remembers the two selection and when the user adds a new select box, that select box takes the value of the first select box which it shouldn't. I would be very grateful if someone could tell me how to solve this problem. I've attached the code below.



<HTML>
<HEAD>
<TITLE> Add/Remove dynamic rows in HTML table </TITLE>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

<SCRIPT language="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 = true;
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>


</HEAD>
<BODY>

<INPUT type="button" value="Add Row" onclick="addRow('dataTable');clearText();" />
<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />

<TABLE id="dataTable" width="350px" border="1">
<form name="form1" method="post" action="<?=$_SERVER['PHP_SELF'];?>">
<TR>
<TD><INPUT type="checkbox" name="chk"/></TD>
<TD>
<SELECT NAME="from" class="xmedium-input" id="from">
<option value="0" selected>Choose port</option>
<?php
$result=mysql_query("SELECT DISTINCT id,port FROM ports ORDER BY port ASC");
while(list($id,$port)=mysql_fetch_row($result)){
echo "<option value=\"$id\"";
if($_POST[from] == $id) echo " selected";
echo ">$port</option>";
}
?>
</SELECT>
</TD>
<TD><INPUT type="text" name="txt"/></TD>
</TR>
<tr>
<TD><INPUT type="checkbox" name="chk"/></TD>
<TD>
<SELECT NAME="to" class="xmedium-input" id="to">
<option value="0" selected>Choose port</option>
<?php
$result=mysql_query("SELECT DISTINCT id,port FROM ports ORDER BY port ASC");
while(list($id,$port)=mysql_fetch_row($result)){
echo "<option value=\"$id\"";
if($_POST[to] == $id) echo " selected";
echo ">$port</option>";
}
?>
</SELECT>
</TD>
<TD>
<?php
if(isset($_POST[Submit])){
$result=mysql_query("SELECT d.id,p1.port,p2.port,d.distance FROM port_to_port AS d LEFT JOIN ports AS p1 ON p1.id=d.from_port LEFT JOIN ports AS p2 ON p2.id=to_port WHERE d.from_port = $_POST[from] AND d.to_port=$_POST[to];");
list($id,$from,$to,$distance)=mysql_fetch_row($result);
}
?>
<INPUT type="text" name="txt" value="<?=$distance;?>"/></TD>
</tr>
<input type="submit" name="Submit" id="submit" class="button" value="Προβολή">
<form>
</TABLE>

</BODY>
</HTML>