I'm trying to get values from an HTML table using PHP and AJAX.
I have this HTML code where users can add more rows to the markup and the SN
increases accordingly, thereby increasing the index value of the name attributes for the input tags. For example:
<td class="int"><input type="text" name="balance[1]" class="field"></td>
becomes:
<td class="int"><input type="text" name="balance[2]" class="field"></td>
after the increment.
The code for adding more rows is as follows:
JS:
function appendRows(e){
var amountRow = $("#moreRows").val();
$.ajax({
type: "POST",
url: "k.php",
data: "days="+days+"&amount="+amountRow,
success: function(response){
console.log(amountRow);
//print the rows returned
$("#rows").append(response);
$("#moreRows").val('');
return;
}
});
}
var more = document.querySelector("#moreRows");
more.addEventListener( 'keydown', function(e){ if(e.keyCode == 13 ) {appendRows()} });
PHP (k.php):
$row_num = $_POST['amount'];
$days = $_POST['days']; //build days
$daysHtm = "";
$rows = "";
$sn = $_SESSION['x_count'];
for($i = 1; $i <= $row_num; $i++){
$sn++; //increment x
$_SESSION['x_count'] = $sn;
for($day = 1; $day <= $days; $day++){
$daysHtm .= "<td class='int day'>
<input type='text' name='day[".$day."][".$sn."]' class='day field'>
</td>";
}
$rows .= '<tr class="trow"><td>'.$sn.'</td><td>
<input type="text" name="item['.$sn.'] class="field"></td><td>
<input type="text" name="type['.$sn.']" class="field"></td>
<td class="int"><input type="text" name="qtybf['.$sn.']" class="field"></td>
<td class="int">
<input type="text" name="qtyrec['.$sn.']" class="field"></td><td class="int">
<input type="text" name="balance['.$sn.']" class="field"></td>';
$rows .= $daysHtm; $daysHtm = "";
$rows .= '<td class="int"><input type="text" name="qtyreturned['.$sn.']" class="field">
</td>
<td class="int">
<input type="text" name="total['.$sn.'] class="field" id="total-'.$sn.'" disabled>
</td>
<td class="int">
<input type="text" name="qtycf['.$sn.']" class="field" id="qtycf-'.$sn.'" disabled>
</td></tr>';
}
echo $rows;
Now the problem is that in my PHP code, the $_POST
variable only contains the first row. i.e.:
<td class="int"><input type="text" name="balance[1]" class="field"></td>
and others with index value of [1]
.
How can I correct this to get the value of other rows? Thanks.
HTML is as follows:
<table class="table table-striped table-sm">
<thead>
<tr id="thead">
<th>S/N</th>
<th>Item</th>
<th>Type/Brand</th>
<th>Quantity B/F</th>
<th>Quantity Recieved</th>
<th>Balance</th>
<th>Quantity Returned</th>
<th>Total Issued</th>
<th>Quantity C/F</th>
</tr>
</thead>
<form action="save.php" method="POST" id="spreadsheet">
<tbody id="rows">
<tr class="trow">
<td>1</td>
<td><input type="text" name="item[1]" class="field"></td>
<td><input type="text" name="type[1]" class="field"></td>
<td class="int"><input type="text" name="qtybf[1]" class="field"></td>
<td class="int"><input type="text" name="qtyrec[1]" class="field"></td>
<td class="int"><input type="text" name="balance[1]" class="field"></td>
<td class="int"><input type="text" name="qtyreturned[1]" class="field"></td>
<td class="int"><input type="text" name="total[1]" class="field" id="total-1" disabled></td>
<td class="int"><input type="text" name="qtycf[1]" class="field" id="qtycf-1" disabled></td>
</tr>
</tbody>
</form>
</table>
<div class="col-lg-12">
<label>Add more rows:</label>
<input type="number" name="moreRows" id="moreRows" placeholder="10">
</div>
I have jQuery as follows:
$("#spreadsheet").on('submit', (function(e){
e.preventDefault();
$.ajax({
type: 'POST',
url: $(this).attr('action'),
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success: function(response){
$("#result").append(response);
},
error: function(){
console.log("ERROR occured while trying to upload with ajax");
}
});
return;
}));
And PHP (save.php):
print_r($_POST); //only returns the first row