在服务器上调用PHP函数时找不到JS函数,但在本地服务器上却能正常工作。

问题描述 投票:-1回答:1

正如标题中所说,我的代码在本地工作,但在服务器上不工作,更准确地说,当我调用任何PHP函数时,它找不到我的JS函数。

总结一下:这里的目标是通过从数据库中获取数据来创建HTML表格,并生成一个excel(.XLS)文件。一切都在本地的Wamp服务器上完美无瑕,但是当我把它移到服务器上,当调用JS函数getDashboardResumeJ()时,我得到了这个错误。

DashboardResume.php:124 Uncaught ReferenceError: getDashboardResumeJ is not defined

如果我删除主文件中的所有PHP,在服务器上一切都能正常工作。

主文件中的PHP。

<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:主文件中的JS。

<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函数被主文件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;

 }
 }

?>

如果你需要更多的片段或信息,不要犹豫。

谢谢你的帮助,祝你有一个美好的一天

javascript php sql database
1个回答
0
投票

对于有同样问题的人,我工作的服务器和我在本地工作的服务器的PHP版本不一样。

试着用phpinfo()检查本地和服务器上的PHP版本是否相同。

© www.soinside.com 2019 - 2024. All rights reserved.