consider code below:
//Consider the php part(process.php)
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Language</th>
</tr>
</thead>
<tbody>
<?php foreach($_POST['names'] as $k=>$v){ ?>
<tr>
<td><?php echo $v; ?></td>
<td><?php echo $_POST['languages'][$k]; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
//html part``
<HTML>
<HEAD>
<TITLE> Add/Remove dynamic rows in HTML table </TITLE>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script language="javascript">
function name_loop(){
var names_array = [];
$('.name_class').each(function(index){
names_array[index] = $.trim($(this).val());
});
return names_array;
}
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
cell2.innerHTML = rowCount + 1;
var cell3 = row.insertCell(2);
var element2 = document.createElement("input");
element2.type = "text";
cell3.appendChild(element2);
}
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) {
table.deleteRow(i);
rowCount--;
i--;
}
}
}catch(e) {
alert(e);
}
}
</SCRIPT>
</HEAD>
<BODY>
<!--<form action="process.php" method="post">-->
<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
<TABLE id="dataTable" width="350px" border="1">
<TR>
<TD><INPUT type="checkbox" name="chk"/></TD>
<TD> 1 </TD>
<TD> <INPUT type="text" class="name_class" name="txt" /> </TD>
</TR>
<input type="submit" name="btn_submit" id="btn_submit" value="check" onclick="send_off();"/>
</TABLE>
<!--</form>-->
<div id="result"></div>
<script type="text/javascript">
function send_off(){
var names = name_loop(); //stores the array of names
//var languages = lang_loop(); //stores the array of languages
$('#result').load('process.php', {'names' : names}); //submit to php script
$('input[type=text]').val(''); //empty all textbox
}
</script>
</BODY>
</HTML>
the problem is thea it only prints one row i.e does not loop through the array printing each element. what is the problem ?