Php 根据单击事件向url添加变量
首先让我简要概述一下我的目标:我希望构建一个基于颜色的密码。更详细地说,有一个750x150px的盒子,里面有两行150x150的正方形。这些正方形中的每一个都随机分配了10种颜色中的一种。目标是让用户按预设的相同顺序单击这些颜色 我已经构建了css和html,确定每个div背景颜色的php已经完成。我现在的问题是如何确定用户选择的div(以及颜色)以及它们的选择顺序 我的目标是将颜色代码添加到url,但我不知道如何做到这一点。有人能解释一下如何做到这一点,或者用另一种方法来组织用户的颜色选择吗 另外,还有一个问题,我如何确保每种颜色都以随机div显示?目前,一些人不会出现,而另一些人最终会出现在两个分区中 目前,我的整个文档都在一个页面中,因此div颜色的变量可以用于索引中的一些if语句 所以如果你有任何建议或代码给我,请帮助 index.php:Php 根据单击事件向url添加变量,php,html,css,url,variables,Php,Html,Css,Url,Variables,首先让我简要概述一下我的目标:我希望构建一个基于颜色的密码。更详细地说,有一个750x150px的盒子,里面有两行150x150的正方形。这些正方形中的每一个都随机分配了10种颜色中的一种。目标是让用户按预设的相同顺序单击这些颜色 我已经构建了css和html,确定每个div背景颜色的php已经完成。我现在的问题是如何确定用户选择的div(以及颜色)以及它们的选择顺序 我的目标是将颜色代码添加到url,但我不知道如何做到这一点。有人能解释一下如何做到这一点,或者用另一种方法来组织用户的颜色选择吗
<?php "session_start" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>colorlock</title>
</head>
<style media="screen" type="text/css">
<?php
$background_colors = array(
"#FFFF00",
"#FF3399",
"#FF0000",
"#990099",
"#66FFFF",
"#339900",
"#000000",
"#000033",
"#FFFFCC",
"#FF3399",
);
$rand_background1 = $background_colors[array_rand($background_colors)];
$rand_background2 = $background_colors[array_rand($background_colors)];
$rand_background3 = $background_colors[array_rand($background_colors)];
$rand_background4 = $background_colors[array_rand($background_colors)];
$rand_background5 = $background_colors[array_rand($background_colors)];
$rand_background6 = $background_colors[array_rand($background_colors)];
$rand_background7 = $background_colors[array_rand($background_colors)];
$rand_background8 = $background_colors[array_rand($background_colors)];
$rand_background9 = $background_colors[array_rand($background_colors)];
$rand_background10 = $background_colors[array_rand($background_colors)];
?>
#full {
height: 300px;
width: 750px;
}
#boxone1 {
height: 150px;
width: 150px;
background: <?=$rand_background1?>;
float: left;
}
#boxone2 {
height: 150px;
width: 150px;
background: <?=$rand_background2?>;
float: left;
}
#boxone3 {
height: 150px;
width: 150px;
background: <?=$rand_background3?>;
float: left;
}
#boxone4 {
height: 150px;
width: 150px;
background: <?=$rand_background4?>;
float: left;
}
#boxone5 {
height: 150px;
width: 150px;
background: <?=$rand_background5?>;
float: left;
}
#boxtwo1 {
height: 150px;
width: 150px;
background: <?=$rand_background6?>;
float: left;
}
#boxtwo2 {
height: 150px;
width: 150px;
background: <?=$rand_background7?>;
float: left;
}
#boxtwo3 {
height: 150px;
width: 150px;
background: <?=$rand_background8?>;
float: left;
}
#boxtwo4 {
height: 150px;
width: 150px;
background: <?=$rand_background9?>;
float: left;
}
#boxtwo5 {
height: 150px;
width: 150px;
background: <?=$rand_background10?>;
float: left;
}
</style>
<body>
<div id="full">
<div id="boxone1" onclick="window.location='?name='+this.id" style=""></div>
<div id="boxone2" onclick="window.location='?name='+this.id" style=""></div>
<div id="boxone3" onclick="window.location='?name='+this.id" style=""></div>
<div id="boxone4" onclick="window.location='?name='+this.id" style=""></div>
<div id="boxone5" onclick="window.location='?name='+this.id" style=""></div>
<div id="boxtwo1" onclick="window.location='?name='+this.id" style=""></div>
<div id="boxtwo2" onclick="window.location='?name='+this.id" style=""></div>
<div id="boxtwo3" onclick="window.location='?name='+this.id" style=""></div>
<div id="boxtwo4" onclick="window.location='?name='+this.id" style=""></div>
<div id="boxtwo5" onclick="window.location='?name='+this.id" style=""></div>
</div>
</div>
<?php
if( $rand_background5 == $rand_background6)
{
echo ("that was lucky!");
}
else
{
echo ("that was expected!");
}
?>
</body>
</html>
色锁
#完全{
高度:300px;
宽度:750px;
}
#boxone1{
高度:150像素;
宽度:150px;
背景:;
浮动:左;
}
#boxone2{
高度:150像素;
宽度:150px;
背景:;
浮动:左;
}
#boxone3{
高度:150像素;
宽度:150px;
背景:;
浮动:左;
}
#boxone4{
高度:150像素;
宽度:150px;
背景:;
浮动:左;
}
#BoxOne 5{
高度:150像素;
宽度:150px;
背景:;
浮动:左;
}
#boxtwo1{
高度:150像素;
宽度:150px;
背景:;
浮动:左;
}
#boxtwo2{
高度:150像素;
宽度:150px;
背景:;
浮动:左;
}
#boxtwo3{
高度:150像素;
宽度:150px;
背景:;
浮动:左;
}
#boxtwo4{
高度:150像素;
宽度:150px;
背景:;
浮动:左;
}
#boxtwo5{
高度:150像素;
宽度:150px;
背景:;
浮动:左;
}
您是否特别需要跟踪URL中的点击?否则,最好在JS数组中静默地跟踪进度
var sequence = [];
$('#full > div').on('click', function() {
sequence.push($(this).attr('id'));
});
然后,单击3次后,序列中会有3个条目
数组-3个div ID
至于确保每种颜色只输出一次,这里的诀窍不仅是从数组中取出一个随机条目,而且还要在以后将其从数组中删除,这样就不能再次拾取
因此,如果以后需要引用颜色数组,可能需要先复制一份
$cols = array('#f00', '#00f', '#0f0', '#d70');
function getRandomColour() {
global $cols;
$num_cols = count($cols);
$rand = array_rand($cols);
$rand_col = $cols[$rand];
echo $rand_col;
unset($cols[$rand]);
}
echo getRandomColour();
不一定在url中。我只是还没学会JS。所以你回答的语法对我来说是胡言乱语。但我会研究一下,看看我能用它做些什么。我的答案大部分是PHP。JS的4行只是设置了一个空数组,然后,在每次单击时,被单击的方块被记录到该数组中。所以在任何时候,为了查看他们点击的顺序,这个数组都知道。尝试一下-只需
console。单击几下后记录数组,查看它包含的内容。好的,谢谢。我唯一的另一个问题是,密码需要依赖于每个div中的颜色,谁会(或可以)将每个随机颜色与它所在的div关联?当我放入js时,我得到一个错误:uncaughtreferenceerror:$未定义如何修复?对不起,我以前没有用过js