I need to save a dynamic table data generated at the client side into the database.
My dynamic table is as follows :
<table class="table" id = "myTable">
<thead>
<tr>
<th>Roll No</th>
<th>Student Name</th>
<th>Attendance</th>
</tr>
</thead>
<tbody>
<?php foreach($results as $students) {?>
<tr id="<?= $students->roll_no;?>" align ="center">
<td><?= $students->roll_no;?></td>
<td><?= $students->full_name;?></td>
<td><input type="radio" id="att" name="attendance" value="present">Present
<input type="radio" id="att" name="attendance" value="absent">Absent</td>
</tr>
<?php } ?>
</tbody>
</table>
<input type="submit" onClick="savedata()" name="submit" value="SAVE">
The twist here is that I am using a radio button in my third column and hence need to pass the checked value on SUBMIT
The javascript code (Reference : Stackoverflow Answer)I am using is :
function savedata() { var oTable = document.getElementById('myTable'); //gets table
var rowLength = oTable.rows.length; //gets rows of table for (i = 1; i < rowLength; i++){ //loops through rows var oCells = oTable.rows.item(i).cells; //gets cells of current row var cellLength = oCells.length; for(var j = 0; j < cellLength; j++){ //loops through each cell in current row <!--get your cell info here--> if(j == cellLength-1) { var cellVal = $('input[name="attendance"]:radio:checked'); alert(cellVal); // store it in array here radioarr = [oTable.rows.item(i).cells,oCells.item(j).innerHTML]; } else { var cellVal = oCells.item(j).innerHTML; alert(cellVal); // store it in array here fieldarr = [oTable.rows.item(i).cells,oCells.item(j).innerHTML]; } } } } $.ajax{ // Pass the data to another page insert.php and store it in the database } </script>
So what I need to do is.. Make a key value pair and store it in an array and pass it as an ajax request and insert the same in the database.
Issues :
The checked radio button is not getting stored in the array
I need to create a 2D array and store all the data there and wen I created its showing [object,object]
I need to pass this array using an ajax request into another page and store it in the database.
Please help me how can I do this ?
Thanks in advance. NC