Fix trailing comma in JavaScript array from PHP foreach

BUGFIX: Uncaught SyntaxError expected expression, got ')'

PROBLEM: Trailing comma after last array item caused JavaScript syntax error

ROOT CAUSE: PHP foreach loop always added comma after each object:
{...},
{...},  // <-- trailing comma before ]
];

While modern JS allows trailing commas, some parsers or contexts
(like when injected/evaluated) don't handle it well.

SOLUTION: Only add comma between items, not after last item:
1. Count total items before loop
2. Track current index in loop
3. Add comma only if currentIndex < itemCount

GENERATED JS NOW:
[
    {...},
    {...},
    {...}   // <-- no trailing comma
]

This eliminates the syntax error.
This commit is contained in:
DevSoftTech Server 2026-06-06 06:00:25 +05:30
parent f6749d2664
commit d3e50174b5

View File

@ -1670,7 +1670,12 @@ $allergyStatusNoKnown = in_array($allergyStatusNormalized, ['no known drug aller
// Load existing complaints from PHP (if any)
<?php if (!empty($complaint_rows)): ?>
selectedDischargeComplaints = [
<?php foreach ($complaint_rows as $row): ?>
<?php
$itemCount = count($complaint_rows);
$currentIndex = 0;
foreach ($complaint_rows as $row):
$currentIndex++;
?>
{
id: <?= (int) ($row['id'] ?? 0) ?>,
term: <?= json_encode((string) ($row['comp_report'] ?? '')) ?>,
@ -1678,7 +1683,8 @@ $allergyStatusNoKnown = in_array($allergyStatusNormalized, ['no known drug aller
severity: '',
duration: <?= json_encode((string) ($row['comp_remark'] ?? '')) ?>,
date: ''
},
}<?= $currentIndex < $itemCount ? ',' : '' ?>
<?php endforeach; ?>
];
<?php endif; ?>