猜数字游戏
效果图
 
代码
片段一
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>猜数字</title>
<style type="text/css">
h2{
text-align: center;
color:
}
.fie{
width: 500px;
padding-left: 20px;
margin-left: 510px;
margin-top: 40px;
}
width: 480px;
margin-top: 10px;
}
.but>button{
width: 488px;
margin-top: 10px;
background-color:
}
</style>
</head>
<body>
<div>
<h2>猜一猜</h2>
<form action="61.php" method="post">
<fieldset class="fie">
<legend>填入一个1~10的整数</legend>
<div class="numb">
<input id="numb" name="numb" type="text" required="">
</div>
<div class="but">
<button>确认</button>
</div>
</fieldset>
</form>
</div>
</body>
</html>
片段2
<?php
$a = $_POST["numb"];
session_start();
$r = rand(1,10);
if (isset($_SESSION["num"])) {
}else{
$_SESSION["num"]=$r;
}
echo "随机产生的数字为:".$_SESSION["num"]."<br>";
if ($a > $_SESSION["num"]) {
echo "<script>alert('猜大了')</script>";
}else if ($a < $_SESSION["num"]) {
echo "<script>alert('猜小了')</script>";
}else if ($a == $_SESSION["num"]) {
echo "<script>alert('猜对了')</script>";
$_SESSION["num"] = NULL;
}
?>
|