我想将PHP变量值和JavaScript变量相乘并在屏幕上显示它。
这是我的代码(PHP文件):
<?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>
这是我的JavaScript代码:
$.ajax({
type: "GET",
url: "url"
}).done(function (data) {
var abc = JSON.parse(data);
var a=abc['rows'];
});
我希望将PHP变量$var1
和JavaScript变量a
相乘,并将其显示在屏幕上。
答案 0 :(得分:1)
您必须将php变量传递给外部脚本文件 在php文件中:
<script>var var1 = "<?php echo $var1; $?>"</script>
然后在脚本文件中,您可以使用此var1变量
并且您的脚本文件必须在php脚本意味着在页脚
之后加载答案 1 :(得分:0)
以下是您想要的确切代码。乐于帮助 :)
<?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>