Currently, my code involves PHP, JSP, and JSON. I need to extract the values from textboxes so that I can insert them into the database.
Specifically, I am dealing with a table that stores information about siblings. Since the number of siblings varies, I have set up a table that dynamically adds rows and columns with textboxes when a button is clicked.
Below is the HTML code for the table:
<table id="tbSibling">
<th>Name</th>
<th>Age</th>
<th>Gender</th>
<th>Occupation and Employer</th>
<tr>
<td><input type="text" id="txtSib10" /></td>
<td><input type="text" id="txtSib11" /></td>
<td><input type="text" id="txtSib12" /></td>
<td><input type="text" id="txtSib13" /></td>
</tr>
<tr>
<td id="btnAdd" class="button-add" onclick="insertSibRow();">Add</td>
</tr>
</table>
The script that dynamically adds rows and columns with textboxes is as follows:
<script type="text/javascript">
// Creating rows and columns dynamically for Table Id: tbSiblings
function insertSibRow(){
var table=document.getElementById("tbSibling");
var lastRow=table.rows.length - 1;
var row=table.insertRow(lastRow);
for(var i=0; i<4; i++)
{
var cellName=row.insertCell(i);
var input=document.createElement('input');
input.type='text';
input.id='txtSib' + lastRow + i ;
cellName.appendChild(input);
}
}
</script>
I assign each input an ID using:
input.id='txtSib' + lastRow + i ;
//result: txtSib10, txtSib11, txtSib12, txtSib13
Now, the challenge lies in extracting values from these dynamically created rows and columns so that they can be passed to a PHP page for insertion into the database.
Currently, it only retrieves the first row. To ensure all rows are captured, I aim to determine the total number of rows by creating an array and pushing the values accordingly.
var lastRow=tblSiblings.rows.length;
var arrSiblings = new array();
for(x=0;x>lastRow;x++){
arrSiblings[x] = $("#txtSib10").val();
}
The issue arises with this line:
arrSiblings[x] = $("#txtSib10").val();
If anyone can assist with retrieving values from dynamically generated textboxes across rows and columns, your help would be greatly appreciated. Thank you!