注册.php
$allCaps=array("img01.png", "img02.png", "img03.png"....); // 20 images
shuffle($allCaps); // this is critical point
$fiveCaps = array_slice($allCaps, 0, 5);
<td id="box01">
<?php
shuffle($fiveCaps);
echo "<img src=\"captcha/imgs/".$fiveCaps[0]."\">";
echo "<img src=\"captcha/imgs/".$fiveCaps[1]."\">";
... //all five images from $fiveCaps are placed here
?>
</td>
<td id="box02">
<script type="text/javascript">
$("#box02").load("register.php #box01")
</script> </td>
在 box02 中,我想要(并且期望)与 box01 中的图像相同(只是打乱了顺序)。
但是在 box02 中出现了一些在 box01 中不存在的图像
如果我删除“critical_point_line” - 两个框中的图像是相同的,但当然,始终只有 $allCaps 中的前五张图像。
最佳答案
只使用 php 函数有什么问题?
<?php
$allCaps=array("img01.png", "img02.png", "img03.png"....); // 20 images
shuffle($allCaps); // this is critical point
$fiveCaps = array_slice($allCaps, 0, 5);
function printRandom($caps)
{
shuffle($caps);
foreach($caps as $cap)
{
echo "<img src=\"captcha/imgs/".$cap."\">";
}
}
?>
<td id="box01">
<?php
printRandom($fiveCaps);
?>
</td>
<td id="box02">
<?php
printRandom($fiveCaps);
?>
</td>
您代码中的问题指出您可能不完全理解 jQuery 的选择器; 执行以下代码:
$("#box02").load("register.php #box01")
不会加载register.php
的新实例,而只是复制#box1
的内容(我们刚刚从PHP 代码填充到 #box02
如果你想打乱变量并用 Javascript 打印它,你需要找到一种方法将它从 PHP 传递到 Javascript。 一个很好的方法是使用 JSON (JavaScript Object Notation
关于php - 如何从另一个 div 中获得相同的、只是随机播放的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11948949/