I am developing an accounting system where users can add journal entries with dynamic rows for account, debtor, creditor, description, and acc_serial. However, when I add a new dynamic row and submit the form, the script throws an error:
An error occurred while processing data. Please try again.
This issue seems to arise because the arrays in the POST request (account, debtor, creditor, description, and acc_serial) have mismatched counts.
What I’ve tried:
PHP server-side validation
I added a validation step to ensure that all arrays (account, debtor, creditor, description, and acc_serial) have the same count:
if (count($_POST['account']) !== count($_POST['debtor']) ||
count($_POST['account']) !== count($_POST['creditor']) ||
count($_POST['account']) !== count($_POST['description']) ||
count($_POST['account']) !== count($_POST['acc_serial'])) {
echo '';
exit;
}
This triggers the error message when the counts don’t match.
Debugging POST data
I used var_dump($_POST) to inspect the submitted data. Here’s the output for a problematic request:
array (size=15)
'date' => string '2024-12-09' (length=10)
'entry_num' => string 'JE-0001' (length=7)
'descrip' => string '6565' (length=4)
'company' => string 'sahat' (length=5)
'auth_token' => string '2582f394f3dab3ac3f139d1f7e715d6181ad182a' (length=40)
'serial' => string '3CWZE7B8ZSVIO71Q440HT6HWX1RWKY3B8EL' (length=35)
'j_type' => string 'Manual Entry' (length=15)
'account' =>
array (size=3)
0 => string 'd59f127d86' (length=10)
1 => string 'd5a05ca920' (length=10)
2 => string 'd59fb74cb8' (length=10)
'debtor' =>
array (size=2)
0 => string '2000' (length=4)
1 => string '0' (length=1)
'creditor' =>
array (size=2)
0 => string '0' (length=1)
1 => string '1000' (length=4)
'description' =>
array (size=2)
0 => string 'General Expense' (length=19)
1 => string 'General Expense' (length=19)
'acc_serial' =>
array (size=2)
0 => string 'd59f127d86' (length=10)
1 => string 'd59fb74cb8' (length=10)
Account has 3 items, while debtor, creditor, description, and acc_serial have only 2 items.
PHP loop for processing rows
foreach ($_POST['account'] as $index => $value) {
if (isset($_POST['account'][$index], $_POST['debtor'][$index], $_POST['creditor'][$index], $_POST['description'][$index], $_POST['acc_serial'][$index])) {
$account = $_POST['account'][$index];
$debtor = $_POST['debtor'][$index];
$creditor = $_POST['creditor'][$index];
$description = $_POST['description'][$index];
$acc_serial = $_POST['acc_serial'][$index];
if (empty($account) || empty($description) || empty($acc_serial) || $debtor === "" || $creditor === "") {
echo '';
exit;
}
if (!is_numeric($debtor) || !is_numeric($creditor)) {
echo '';
exit;
}
} else {
echo '';
exit;
}
}
HTML structure for dynamic rows
JavaScript for adding dynamic rows:
$("#add-btn").click(function() {
let i = $("#dynamicAddRemove tbody tr").length;
$("#dynamicAddRemove").append('' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ');
});
What could cause this mismatch in field counts when adding dynamic rows?
How can I debug or fix this issue to ensure all fields are submitted correctly?