As stated in the title my code works in local but not on server. More precisely, when i call whatever PHP function it doesn’t find my JS function.
To sum it up : The goal here is to generate an excel (.XLS) file from an HTML table which is created by retrieving data from a database. Everything works flawlessly in local with my Wamp server but when I move it to server I get this error when calling the JS function getDashboardResumeJ ():
DashboardResume.php:124 Uncaught ReferenceError: getDashboardResumeJ is not defined
If I remove all the PHP in the main file everything works when on server.
PHP in main file :
<button id="buttonXLS" href="#" onclick="exportTableToExcel('tableRecap', 'Rapport Mensuel <?php echo $year . '/' . $month?>')">Export to Excel</button> <table style="display:none" id="tableRecap" > <tr> <td style="font-weight: bold">Nom et prénom</td> <?php $arrayId = array(selectTableEmploye('nom', 'prenom')); ?> </tr> <tr> <td style="font-weight: bold">Date d'entrée</td> <?php selectTableEmploye('date_embauche'); ?> </tr> <tr> <td style="font-weight: bold">Date de sortie</td> <?php selectTableEmploye('date_depart'); ?> </tr> <tr> <td style="font-weight: bold">Remarque 1</td> <?php selectTableTimesheet('commentaire1',$arrayId,$month,$year); ?> </tr> <tr> <td style="font-weight: bold">Remarque 2</td> <?php selectTableTimesheet('commentaire2',$arrayId,$month,$year); ?> </tr> <tr> <td style="font-weight: bold">Remarque 3</td> <?php selectTableTimesheet('commentaire3',$arrayId,$month,$year); ?> </tr> <tr> <td style="font-weight: bold">Remarque 4</td> <?php selectTableTimesheet('commentaire4',$arrayId,$month,$year); ?> </tr> <?php generateDays($dateMonth, $dateYear); ?> </table>
JS in main file :
<script> function exportTableToExcel(tableID, filename = ''){ var downloadLink; var dataType = 'application/vnd.ms-excel'; var tableSelect = document.getElementById(tableID); var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20'); // Specify file name filename = filename?filename+'.xls':'excel_data.xls'; // Create download link element downloadLink = document.createElement("a"); document.body.appendChild(downloadLink); if(navigator.msSaveOrOpenBlob){ var blob = new Blob(['ufeff', tableHTML], { type: dataType }); navigator.msSaveOrOpenBlob( blob, filename); }else{ // Create a link to the file downloadLink.href = 'data:' + dataType + ', ' + tableHTML; // Setting the file name downloadLink.download = filename; //triggering the function downloadLink.click(); } } $(document).ready(function(){ $('.month_dropdown').on('change',function(){ getDashboardResumeJ('dashboard_div', $('.year_dropdown').val(), $('.month_dropdown').val()); }); $('.year_dropdown').on('change',function(){ getDashboardResumeJ('dashboard_div', $('.year_dropdown').val(), $('.month_dropdown').val()); }); getCommJ(); }); function getDashboardResumeJ(target_div, year, month){ $.ajax({ type:'POST', url:'functionsDashboardResume.php', data:'func=getDashboardResumeP&year='+year+'&month='+month, success:function(html){ $('#'+target_div).html(html); } }); }
PHP functions called by main file PHP :
<?php function selectTableEmploye($attribute, $optAttribute = '') { include "dbConfig.php"; $query = 'SELECT * FROM employe ORDER BY id_employe ASC'; if ($stmt = $db->prepare($query)) { /* execute query */ $stmt->execute(); /* Get the result */ $result = $stmt->get_result(); $arrayId = []; if ($optAttribute != '') { while ($row = $result->fetch_assoc()) { echo '<td> ' . $row[$attribute] . ' '; echo $row[$optAttribute] . '</td>'; array_push($arrayId, $row['id_employe']); } } else { while ($row = $result->fetch_assoc()) { echo '<td> ' . $row[$attribute] . '</td>'; } } /* free results */ $stmt->free_result(); } return $arrayId; } function selectTableTimesheet($attribute, $arrayId, $month, $year) { include "dbConfig.php"; if (!empty($arrayId)) { foreach ($arrayId[0] as $value) { $query = 'SELECT DISTINCT commentaire1,commentaire2,commentaire3,commentaire4 FROM timesheet,employe WHERE timesheet.id_employe =' . $value. ' AND timesheet.annee = ' . $year . ' AND timesheet.mois = ' . $month; if ($stmt = $db->prepare($query)) { /* execute query */ $stmt->execute(); /* Get the result */ $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { echo '<td> ' . $row[$attribute] . '</td>'; } } } } else{ $query = 'SELECT * FROM timesheet'; if ($stmt = $db->prepare($query)) { /* execute query */ $stmt->execute(); /* Get the result */ $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { echo '<td> ' . $row[$attribute] . '</td>'; } } }} function generateDays($month, $year) { $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); for ($i = 1; $i <= $daysInMonth; $i++) { if($month != 10){ $monthFunc = trim($month,0); } else{ $monthFunc = 10; } $data = getActivity($i,$monthFunc,$year); $flag_half_day = 0; $flag_row_number = 1; $secondRowContent = array(); $numItems = mysqli_num_rows($data); $checkIndex = 0; echo "<tr>" . "<td style='text-align: right;font-weight: bold;'>" . $i . "</td>" ; while ($row = $data->fetch_assoc()) { if($flag_row_number == 1){ //cas 2b if ($flag_half_day == 1){ array_push($secondRowContent,'<td>' . $row['code_fiduciaire'] . $row['nombre_heure'] . '</td>'); $flag_half_day = 0; } else{ //cas 1 if($row['nombre_heure'] == 8){ echo '<td>' . $row['code_fiduciaire'] . $row['nombre_heure'] . '</td>'; array_push($secondRowContent,'<td></td>'); $flag_half_day = 0; } //cas 2a else if(is_null($row['nombre_heure'])){ echo '<td></td>'; } else{ echo '<td>' . $row['code_fiduciaire'] . $row['nombre_heure'] . '</td>'; $flag_half_day = 1; } } if($checkIndex++ == ($numItems - 1)){ $flag_row_number = 2; echo "</tr>"; } } } if($flag_row_number == 2){ echo '<tr> <td style="text-align: right;font-weight: bold;">' . $i . '</td>'; foreach($secondRowContent as $content){ echo $content; } echo '</tr>'; } else{ echo '<tr> <td style="text-align: right;font-weight: bold;">' . $i . '</td> </tr>'; } } } function getActivity($day, $month, $year){ include "dbConfig.php"; $query = 'select time_dimension.db_date, time_dimension.holiday_flag, employe.id_employe, employe.nom, timesheet.nombre_heure, timesheet.date, taches.code_fiduciaire FROM time_dimension left outer join employe on time_dimension.db_date >= employe.date_embauche left outer join timesheet on timesheet.date = time_dimension.db_date AND timesheet.id_employe = employe.id_employe left outer join taches on timesheet.id_tache = taches.id_tache where time_dimension.year = ' . $year . ' and time_dimension.month = ' . $month . ' and time_dimension.day = ' . $day . ' AND COALESCE (timesheet.nombre_heure,1) != 0 ORDER BY employe.id_employe, time_dimension.db_date' ; if ($stmt = $db->prepare($query)) { /* execute query */ $stmt->execute(); /* Get the result */ $result = $stmt->get_result(); return $result; } } ?>
If you need more snippets or informations don’t hesitate.
Thanks for your help, Have a great day
Advertisement
Answer
For those who have the same kind of problem, the server I was working on didn’t have the same PHP version as the one I was working in local.
Try phpinfo() both in local and on your server to check your PHP version is the same.