Show Mysql data inside Jquery

Asked

Viewed 134 times

0

I have a file called somar.js which contains some Jquery commands, among them the sum of the packages * daily exchange value, but these values come from the database. To prevent the administrator from manually registering on the system and this 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 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);
        }
  • Inside a *.js file, it is not possible to put php commands. If the script is in html and not in a . js file, it is possible. In your case it would be interesting to create a global javascript variable and load the value of the database into it.

  • Hi Jhonatan, but how would I load the value of the bank? I am a little layman in Jquery.

  • Good inside the js file without chance. You already have the connection of the database and the variable php with the value? If yes put the solution I thought for you

  • Hi Jhonatan, I do. The system was developed in PHP/Mysql.

1 answer

1


Well, see if it helps you with the proposed solution.

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

Remove the js file from your page and enter the code below <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 ( Set a global js that takes the php value and then picks it up in the file ):

Add the code below between the tags <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 watch out to include *.js below the global setting or use the $(document).ready()

  • @Romario Pires the proposed suggestion is irrelevant in view of the fact that <?=$variavel;?> works the same way <?php echo $variavel;?>

  • Hello Jhonatan, actually I had thought to put this way, but the js code is great and I think it would not be nice to put directly on the page. There is no Jquery plugin that does this?

  • 1

    @Jose.Marcos includes another solution in the answer. See if you can understand and if it works.

  • Bingo Jhonatan in the second solution. Thank you very much and already want to wish you a Happy 2016 with lots of prosperity, health and peace. Hug.

  • Happy 2016 to you too bro

Browser other questions tagged

You are not signed in. Login or sign up in order to post.