0

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.

2
  • 1
    Is your JavaScript output by the same PHP file, or is it in an external .js file? Commented Mar 10, 2018 at 3:50
  • external js file Commented Mar 10, 2018 at 4:07

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Is the syntax correct?because it says empty statement
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.