javascript - PHP随机页面url自动重新加载

标签 javascript php jquery html random

我试图让下面的 php 脚本自动将页面快速重新加载到包含网站页面的文件夹中的随机页面。我的代码仅显示文本链接或不显示任何内容,并且不会自动重新加载到随机页面。这是代码:

<?php 

//set the urls 
$urls = array("1.html" ,"2.html" ,"3.html" ,"4.html" ,"5.html" ,"6.html" ,"7.html" ,"8.html" ,"9.html" 
    ,"10.html" ,"11.html" ,"12.html" ,"13.html" ,"14.html" ,"15.html" ,"16.html" ,"17.html" ,"18.html" ,
    "19.html" ,"20.html" ,"21.html" ,"22.html" ,"23.html" ,"24.html" ); 

//set the number in (rand()%3); for however many links there are 
        $random = (rand()%24); 
echo ("<a href = \"$urls[$random]"); 

?>

因此,如果我有一个用户位于 3.html 的页面,并且他们单击一个按钮,我希望脚本运行并从此数组中加载另一个随机页面,并在 URL 框中立即将页面重新加载到该页面。

3.html 看起来像这样,这里是按钮自动页面加载 javascript 的代码:

<h2 id="correct">CORRECT</h2>
<h2 id="wrong">WRONG</h2>

<div id="answers">
    <button onclick="document.getElementById('wrong').style.visibility='visible';
                 window.location = 'random.php';" id="one">1986</button>
                <button onclick="document.getElementById('correct').style.visibility='visible'; 
                 window.location = 'random.php';" id="two">1913</button>
                <button onclick="document.getElementById('wrong').style.visibility='visible';
                 window.location = 'random.phprandom.php';" id="three">1723</button>
                <button onclick="document.getElementById('wrong').style.visibility='visible';
                 window.location = 'random.php';" id="four">1812</button>
</div>

请帮我修复代码!

此外,如果链接用数字标记,是否有更简单的方法将这些页面添加到数组中。这意味着我可以用一行代码来完成此操作吗?

最佳答案

删除回显线并将其替换为:

header("Location: ".$urls[$random]);
exit;

如果您知道所有页面都是数字,您可以执行以下操作:

$url = rand(1,24).'.html';
header("Location: ".$url);
exit;

关于javascript - PHP随机页面url自动重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28288538/

相关文章:

javascript - JQuery:在元素的 HTML 之前附加项目

html - 使用 jQuery toggle() 后如何响应式地隐藏菜单?

JQuery 子级数

php - 在wordpress中使用php和ajax将数据插入mysql数据库

jquery - 动态数据表分页和排序与变化/动态内容

javascript - jQuery.Validate - 输入后验证

javascript - 当数组包含基本多语言平面之外的字符时,为什么 Array#slice 无法按预期工作?

javascript - 使用 webpack,我可以将 Node.js 和浏览器代码放在同一个文件中,但阻止 Node.js 代码进入浏览器吗?

php - Laravel 关系表用户和地址

php - 如何替换 html 表格 onchange 的单列