我在将变量从我的 javascript 函数传递到我的 php 页面时遇到了一些麻烦。open_boosters 从我想传递给 site3.php 的数组中返回一个随机变量。我知道 PHP 代码首先被执行,但是如何在用户不必点击任何东西的情况下传递这些变量?
> Main.html
<p id="generatenumbers"></p>
<script>
var open_boosters = function()
{
var i, j;
var array=[];
for(i=0; i < 13; i++)
{
(array[i] = Math.floor(Math.random()*14)+16);
for(j=0;j<i;j++)
{
while(array[i]==array[j])
{
(array[i]= Math.floor(Math.random()*14)+16);
}
}
return array[i];
}
}
window.addEventListener('DOMContentLoaded', function ()
{
var randnumb = open_boosters();
document.getElementById("generatenumbers").innerHTML=open_boosters();
document.getElementById("randImg").src = "site3.php?rndNum"+randnumb;
}, false);
</script>
站点3.php
<?php
$mysqli=mysqli_connect('localhost','root','','draftdb');
if (!$mysqli)
die("Can't connect to MySQL: ".mysqli_connect_error());
$stmt = $mysqli->prepare("SELECT display.PICTURE_ID
FROM cards
INNER JOIN display ON cards.DISPLAY_ID = display.DISPLAY_ID
WHERE display.DISPLAY_ID=? AND cards.CARD_TYPE ='rare'" );
$displayid = isset($_GET['randnumb'])? $_GET['randnumb'] : false;
//echo "Number: ".$displayid." ";
$stmt->bind_param("i", $displayid);
$stmt->execute();
$stmt->bind_result($image);
$stmt->fetch();
//header("Content-Type: image/jpeg");
echo $image;
?>