Kakne Prateek
Kakne Prateek

Reputation: 1

Multiply JavaScript variable and PHP variable and display on the screen

I want to multiply a PHP variable value and a JavaScript variable and display it on the screen.

Here's my code (PHP file):

<?php
require 'config.php';
session_start();
?>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="knockout.js"></script>
        <script src="hello.js" defer="defer"></script>
    </head>
    <body>
        
        <?php
        $uname = $_SESSION['username'];
        $query = "select * abc where username='$uname'";
        $query_run = mysqli_query($con, $query);
        if (mysqli_num_rows($query_run) == 1) {

            while ($rows = mysqli_fetch_array($query_run)) {

                $var1 = $rows['row1'];
                $var1= (int) $var1;
                $_SESSION['var1'] = $var1;
            }
        } else {
            echo $uname;
        }
        ?>

    </body>
</html>

Here's my JavaScript code:

$.ajax({
    type: "GET",

    url: "url"
}).done(function (data) {
    var abc = JSON.parse(data);
    var a=abc['rows'];

});

I want to multiply the PHP variable $var1 and the JavaScript variable a and display it on the screen.

Upvotes: 0

Views: 860

Answers (2)

Rahul Mehra
Rahul Mehra

Reputation: 720

Below is the exact code you want. Happy to help :)

<?php
require 'config.php';
session_start();
?>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="knockout.js"></script>
        <script src="hello.js" defer="defer"></script>
    </head>
    <body>
        
        <?php
        $uname = $_SESSION['username'];
        $query = "select * abc where username='$uname'";
        $query_run = mysqli_query($con, $query);
        if (mysqli_num_rows($query_run) == 1) {

            while ($rows = mysqli_fetch_array($query_run)) {

                $var1 = $rows['row1'];
                $var1= (int) $var1;
                $_SESSION['var1'] = $var1;
            }
        } else {
            echo $uname;
        }
        ?>
        
        <script>var var1 = <?php echo $var1; $?></script>
        <script>
        
          $.ajax({
              type: "GET",
              url: "url"
          }).done(function (data) {
              var abc = JSON.parse(data);
              var a=abc['rows'];
              var result = var1 * a;//now display result variable in the page wherever you want
              document.getElementById("id_of_element_where_you_want_display_result").innerHTML= result;

          });
          
           
        </script>
    </body>
</html>

Upvotes: 0

Priya
Priya

Reputation: 1470

You have to pass php variable to external script file in php file:

<script>var var1 = "<?php echo $var1; $?>"</script>

And then in script file you can use this var1 variable

And your script file must load after php script means in footer

Upvotes: 1

Related Questions