Show Mysql data inside JQuery

0

I have a file called somar.js where it contains some JQuery commands, including the sum of the packets * daily exchange value, but these values come from the database. To prevent the administrator from registering on the system and manually in that file, I would like to know if it is possible to bring mysql results inside JQuery or insert PHP commands into js files. See below the section that I need this solution:

var taxaCambio = 4.20; // Nessa linha

        var totalGeralSomar = total1 + total2 + total3 + total4;
        var totalCambio = totalGeralSomar * taxaCambio;
        if(totalGeralSomar.toFixed(2) == "NaN" || totalCambio.toFixed(2) == "NaN"){
            document.getElementById("totalGeral").innerHTML = "R$ 0.00";
            document.getElementById("subTotal").innerHTML = "USD 0.00";
        }else{

            Number.prototype.formatMoney = function(c, d, t){
                var n = this, 
                    c = isNaN(c = Math.abs(c)) ? 2 : c, 
                    d = d == undefined ? "." : d, 
                    t = t == undefined ? "," : t, 
                    s = n < 0 ? "-" : "", 
                    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", 
                    j = (j = i.length) > 3 ? j % 3 : 0;
                   return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
                 };

             document.getElementById("totalGeral").innerHTML = "R$ " +(totalCambio).formatMoney(2, ',', '.');;
            document.getElementById("subTotal").innerHTML = "USD " +totalGeralSomar.toFixed(2);
        }
    
asked by anonymous 28.12.2015 / 15:05

1 answer

1

Well, see if the proposed solution helps you.

Solution 1 (Put javascript on your page without being in an external file):

Remove the js file from your page and put the code below between <script type="text/javascript"></script>

    var taxaCambio = <?=$suaVariavelCambio;?>; // Substituir por sua variavel PHP
    var totalGeralSomar = total1 + total2 + total3 + total4;
    var totalCambio = totalGeralSomar * taxaCambio;
    if(totalGeralSomar.toFixed(2) == "NaN" || totalCambio.toFixed(2) == "NaN"){
        document.getElementById("totalGeral").innerHTML = "R$ 0.00";
        document.getElementById("subTotal").innerHTML = "USD 0.00";
    }else{

        Number.prototype.formatMoney = function(c, d, t){
            var n = this, 
                c = isNaN(c = Math.abs(c)) ? 2 : c, 
                d = d == undefined ? "." : d, 
                t = t == undefined ? "," : t, 
                s = n < 0 ? "-" : "", 
                i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", 
                j = (j = i.length) > 3 ? j % 3 : 0;
               return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
             };

         document.getElementById("totalGeral").innerHTML = "R$ " +(totalCambio).formatMoney(2, ',', '.');;
        document.getElementById("subTotal").innerHTML = "USD " +totalGeralSomar.toFixed(2);
    }

Solution 2 (Define a global js that gets the value of php and then get it in the file):

Add the code below between the <script type="text/javascript"></script>

var Globais = {}; // Definir a global Globais.taxaCambio = <?=$suaVariavelCambio;?>;

Then inside your JS file change to:

    var taxaCambio = Globais.taxaCambio; // Substituir por sua variavel PHP
    var totalGeralSomar = total1 + total2 + total3 + total4;
    var totalCambio = totalGeralSomar * taxaCambio;
    if(totalGeralSomar.toFixed(2) == "NaN" || totalCambio.toFixed(2) == "NaN"){
        document.getElementById("totalGeral").innerHTML = "R$ 0.00";
        document.getElementById("subTotal").innerHTML = "USD 0.00";
    }else{

        Number.prototype.formatMoney = function(c, d, t){
            var n = this, 
                c = isNaN(c = Math.abs(c)) ? 2 : c, 
                d = d == undefined ? "." : d, 
                t = t == undefined ? "," : t, 
                s = n < 0 ? "-" : "", 
                i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", 
                j = (j = i.length) > 3 ? j % 3 : 0;
               return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
             };

         document.getElementById("totalGeral").innerHTML = "R$ " +(totalCambio).formatMoney(2, ',', '.');;
        document.getElementById("subTotal").innerHTML = "USD " +totalGeralSomar.toFixed(2);
    }

Just be careful to include * .js below the global setting or use $(document).ready()

    
28.12.2015 / 16:44