使用javascript,mysql,php和html打印包含工资计算的网页

时间:2018-04-25 10:23:36

标签: javascript php jquery html css

我正在努力为员工创建工资单。我能够从数据库中检索薪水信息并显示在页面上。我还可以使用javascript函数从工资中计算叶子工资扣除额并将其传递给html页面。但是,当我通过打印机通过页面上的打印按钮发出打印命令时,不会打印假工资扣除值。我将非常感谢这方面的任何帮助,因为我几天都在努力解决这个问题。

HTML代码:

echo '<div id = "divToPrint">';

        echo '<form name = "formName1"><table>';

echo '<tr><td colspan = "9"><B>Salary Break Up:</B></td></tr>';

            echo '<tr><td colspan = "6">Basic:</td><td>'.$rows[13].'</td><td></td></tr>';
            echo '<tr><td colspan = "6">DA:</td><td>'.$rows[14].'</td><td></td></tr>';
            echo '<tr><td colspan = "6">HR:</td><td>'.$rows[15].'</td><td></td></tr>';

            $a = $rows[13]+$rows[14]+$rows[15];

            echo '<td colspan = 7"><B>Gross Salary(A):</B></td><td><B>'.$a.'</B></td></tr>';

            echo '<tr><td colspan = "8">&nbsp;</td></tr>';

            echo '<tr><td colspan = "8"><B>Deductions:</B></td></tr>';

            echo '<tr><td colspan = "6">Provident Fund:</td><td>'.$rows[16].'</td><td></td></tr>';
            echo '<tr><td colspan = "6">Income Tax:</td><td>'.$rows[17].'</td><td></td></tr>';
            echo '<tr><td colspan = "6">EMI:</td><td>'.$rows[18].'</td><td></td></tr>';

            echo '<tr><td colspan = "6">Loan:</td><td>'.$rows[19].'</td><td></td></tr>';
            echo '<tr><td colspan = "6">Other Deduction:</td><td>'.$rows[21].'</td><td></td></tr>';

            $deduction = $rows[16]+$rows[17]+$rows[18]+$rows[19]+$rows[21];

            echo '<tr><td>Leaves Taken</td>';

echo '<td><select name = "leaves_taken" onChange="setForm2Value()">';

                echo '<option value = "">Select...</option>';
                echo '<option value = "0">0</option>';
                echo '<option value = "1">1</option>';
                echo '<option value = "2">2</option>';
                echo '<option value = "3">3</option>';
                echo '<option value = "4">4</option>';

echo '</select></td>';

            echo '<td colspan = "4">Leave Salary Deduction</td>';

            echo '<td><input type = "text" name = "leave_sal_deduction" id = "leave_sal_deduction" border = "0"></td><td></td></tr>';

            echo '<td colspan = 7"><B>Total Deductions(B):</B></td>
            <td><input type = "text" name = "total_deduction" id = "total_deduction"></td></tr>';

            echo '<tr><td colspan = "8">&nbsp;</td></tr>';


            $d = "Net Salary(A - B): ";
            echo '<td colspan = "7"><B>'.$d.'</B></td>';
            echo '<td><input type = "text" name = "total_salary" id = "total_salary"></td></tr>';

        }

    echo '</table></form>';

        echo '</div>';

Javascript代码:

function PrintDiv() {    

var divToPrint = document.getElementById('divToPrint');

var popupWin = window.open('', '_blank');

popupWin.document.open();

popupWin.document.write('<html><body onload="window.print()">');

popupWin.document.write(divToPrint.innerHTML + '</html>');

popupWin.document.close();

            }

使用JavaScript离开薪资计算:

function setForm2Value() {

    var selectedItem      = document.formName1.leaves_taken.selectedIndex;
    var selectedItemValue = document.formName1.leaves_taken.options[selectedItem].value;
    var selectedItemText  = document.formName1.leaves_taken.options[selectedItem].text;

    if (selectedItem != 0) {
        //document.formName1.textboxName1.value = selectedItemText;


        //Getting Gross Salary
        var net_sal = "<?php echo $a ?>";
        var net_sal1 = parseInt(net_sal);


        //Calculation of Leave Salary Deduction
        var abc = ((net_sal1 / 30) * selectedItemValue).toFixed(2);
        var abc1 = parseInt(abc) ;  


        //Printing Leave Salary Deduction
        $("#leave_sal_deduction").val(abc1);

        //Calculating Total Deduction
        var d = "<?php echo $deduction ?>";
        var d1 = parseInt(d);

         var b = parseInt(d1 + abc1);

        //Displaying Total Deduction
        $("#total_deduction").val(b);



        //Calculating Total Payable Salary
        var total = (net_sal1 - b);
        $("#total_salary").val(total);

Print preview of the page through printer. Not Able to get the leave salary deduction and net salary values printed on a page using print method through a printer.

1 个答案:

答案 0 :(得分:0)

设置元素的值不会更新html。更新你可以使用的HTML object.setAttribute 例如,更新元素leave_sal_deduction

的html
document.getElementById('leave_sal_deduction').setAttribute("value",document.getElementById('leave_sal_deduction').value);

您可以在获取divToPrint的html之前将其添加到您的打印功能