HTML部分:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<script src="../../jQuery/jquery-3.4.1.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
section {
width: 230px;
margin: 50px auto;
text-align: center;
overflow: hidden;
}
section>* {
float: left;
}
section input {
width: 150px;
height: 30px;
padding: 0 10px;
border: 1px solid #ccc;
outline: none;
box-sizing: border-box;
font-size: 16px;
}
section input:focus {
border-color: orange;
}
section button {
width: 80px;
height: 30px;
outline: none;
}
section div {
width: 100%;
height: 30px;
text-align: center;
line-height: 30px;
font-size: 13px;
border-radius: 10px;
border-bottom: 1px solid #333;
box-sizing: border-box;
cursor: pointer;
/* cursor: not-allowed; */
}
</style>
</head>
<body>
<section>
<input type="number" name="number" placeholder="你要吃几道菜?">
<button type="button">开始点菜</button>
</section>
<script>
$(function() {
$('section button').on('click', function() {
$('section>div').remove();
$.get('end.php', {
num: $('input').val(),
}, function(e) {
var re = e;
if (re.code == 1001) {
alert(re.message);
} else {
for (var i = 0; i < re.length; i++) {
$('section').append('<div>' + re[i] + '</div>');
};
}
}, 'json');
});
$('section').on('click', "div", function() {
var _this = $(this);
$.post('end.php', {
click: $(this).text(),
}, function(e) {
if (e.code == 200) {
_this.css({
"color": "gray",
"cursor": 'not-allowed'
});
}
}, 'json');
});
});
</script>
</body>
</html>
PHP部分:
<?php
if($_GET){
$num = ((int)$_GET['num']);
$min_cai = 1;
$max_cai = 72;
if($num < $min_cai || $num > $max_cai){
$re = [
'code'=>1001,
'message'=>'数值不在指定范围1~72'
];
echo json_encode($re);
die();
}else{
$cai_arr = explode('&',file_get_contents('end.txt'));
$arr = [];
for($i = 0;$i < $num;$i++){
$cai_num = mt_rand($min_cai-1, $max_cai-1);
while(in_array($cai_num,$arr)){
$cai_num = mt_rand($min_cai-1, $max_cai-1);
}
array_push($arr,$cai_num);
};
$re = [];
foreach($arr as $k => $v){
array_push($re, $cai_arr[$v]);
}
echo json_encode($re);
}
}
elseif($_POST){
$cai_name = $_POST['click'];
$cai_arr = explode('&',file_get_contents('end.txt'));
if(in_array($cai_name,$cai_arr)){
$File = fopen('./data.txt','a+');//这里是你曾品尝过的食物
fwrite($File,$cai_name.'&');
fclose($File);
$re = [
'code' => 200,
'message' => '品尝成功'
];
echo json_encode($re);
}
}
?>
运行效果:
运行效果
储存的数据:
汽锅鸡&酸辣藕丁&蛋饺&家庭版剁椒鱼头&小笼汤包&