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:
parent
f6749d2664
commit
d3e50174b5
|
|
@ -1670,7 +1670,12 @@ $allergyStatusNoKnown = in_array($allergyStatusNormalized, ['no known drug aller
|
||||||
// Load existing complaints from PHP (if any)
|
// Load existing complaints from PHP (if any)
|
||||||
<?php if (!empty($complaint_rows)): ?>
|
<?php if (!empty($complaint_rows)): ?>
|
||||||
selectedDischargeComplaints = [
|
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) ?>,
|
id: <?= (int) ($row['id'] ?? 0) ?>,
|
||||||
term: <?= json_encode((string) ($row['comp_report'] ?? '')) ?>,
|
term: <?= json_encode((string) ($row['comp_report'] ?? '')) ?>,
|
||||||
|
|
@ -1678,7 +1683,8 @@ $allergyStatusNoKnown = in_array($allergyStatusNormalized, ['no known drug aller
|
||||||
severity: '',
|
severity: '',
|
||||||
duration: <?= json_encode((string) ($row['comp_remark'] ?? '')) ?>,
|
duration: <?= json_encode((string) ($row['comp_remark'] ?? '')) ?>,
|
||||||
date: ''
|
date: ''
|
||||||
},
|
}<?= $currentIndex < $itemCount ? ',' : '' ?>
|
||||||
|
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
];
|
];
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user