本地正常线上报错:调用PHP函数时JS函数getDashboardResumeJ未定义
Uncaught ReferenceError: getDashboardResumeJ is not defined in Production First, let's break down the core issue: your code works locally but fails online when PHP is included. This almost always means PHP is outputting unexpected content (errors/warnings) that breaks your HTML/JS structure, preventing the getDashboardResumeJ function from being properly parsed by the browser.
Here are the step-by-step fixes to resolve this:
1. Fix PHP Error Output (Critical!)
Online servers often have different error reporting settings than your local WAMP setup. If your PHP functions throw warnings/errors (like database connection issues, invalid queries), these get printed directly into your HTML, which can:
- Break the
<script>tag containing your JS functions - Corrupt the DOM structure, making the browser fail to parse JS correctly
Solution:
Add error handling at the top of your PHP files to log errors instead of displaying them:
<?php // Enable error logging but disable display to browser error_reporting(E_ALL); ini_set('display_errors', 0); ini_set('log_errors', 1); ini_set('error_log', '/path/to/your/php-error.log'); // Update this path to a writable location ?>
Then check the error log to find exactly what's failing in your PHP functions (e.g., database connection issues in selectTableEmploye or invalid queries in getActivity).
2. Fix Invalid Array Handling in $arrayId
In your main PHP code, you're wrapping the return value of selectTableEmploye in an extra array:
<?php $arrayId = array(selectTableEmploye('nom', 'prenom')); ?>
But selectTableEmploye already returns an array. This creates a nested array ($arrayId[0] is the actual employee IDs), which can cause errors in selectTableTimesheet if the function expects a flat array. If selectTableEmploye returns an empty array, this will lead to PHP warnings.
Solution:
Remove the extra array() wrapper:
<?php $arrayId = selectTableEmploye('nom', 'prenom'); ?>
And update selectTableTimesheet to loop over the flat array directly:
// Inside selectTableTimesheet, replace this: foreach ($arrayId[0] as $value) { // With this: foreach ($arrayId as $value) {
3. Escape Dynamic Content in HTML Attributes
Your export button uses unescaped PHP variables in the onclick attribute:
<button id="buttonXLS" href="#" onclick="exportTableToExcel('tableRecap', 'Rapport Mensuel <?php echo $year . '/' . $month?>')">Export to Excel</button>
If $year or $month contains special characters (like quotes, though unlikely here), it will break the onclick syntax, which can cascade into JS parsing errors.
Solution:
Use htmlspecialchars to escape the dynamic content:
<button id="buttonXLS" href="#" onclick="exportTableToExcel('tableRecap', 'Rapport Mensuel <?php echo htmlspecialchars($year . '/' . $month, ENT_QUOTES); ?>')">Export to Excel</button>
4. Isolate Your JS Code from PHP Output
Mixing PHP echo statements directly into your HTML can lead to unexpected DOM corruption if PHP fails. Move your JS code into a separate file (e.g., dashboard.js) and include it at the end of your <body> tag:
<!-- At the end of your body --> <script src="dashboard.js"></script>
This ensures your JS code is loaded consistently, regardless of any issues with PHP-generated HTML content.
5. Add Error Handling to PHP Database Queries
Your PHP functions like selectTableEmploye don't handle cases where $db->prepare() fails. If the database connection is down or the query is invalid, this will throw warnings that break your page.
Example Fix for selectTableEmploye:
function selectTableEmploye($attribute, $optAttribute = '') { include "dbConfig.php"; $query = 'SELECT * FROM employe ORDER BY id_employe ASC'; if (!$stmt = $db->prepare($query)) { error_log("Failed to prepare query: " . $db->error); // Log the error return []; // Return empty array instead of breaking output } // Rest of your code... }
By addressing these issues, you'll eliminate the PHP-generated content that's breaking your JS function definition in the online environment.
内容的提问来源于stack exchange,提问作者Ustalioth

