使用隐藏字段来保存状态
2015-06-21 20:43
471 查看
numguess2.php:
<?php
$num_to_guess = 42;
$num_tries = (isset($_POST["num_tries"])) ? $_POST["num_tries"] + 1 : 1;
if (!isset($_POST["guess"])) {
$message = "Welcome to the guessing machine!";
} else if ($_POST["guess"] > $num_to_guess) {
$message = $_POST["guess"]." is too big! Try a smaller number.";
} else if ($_POST["guess"] < $num_to_guess) {
$message = $_POST["guess"]." is too small! Try a larger number.";
} else { // must be equivalent
$message = "Well done!";
}
$guess = $_POST["guess"];
?>
<html>
<head>
<title> Saving state with a hidden field</title>
</head>
<body>
<h1><?php echo $message; ?></h1>
<p><strong>Guess number:</strong> <?php echo $num_tries; ?></p>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST">
<p><strong>Type your guess here:</strong>
<input type="text" name="guess" value="<?php echo $guess; ?>" />
<input type="hidden" name="num_tries" value="<?php echo $num_tries; ?>" />
<p><input type="submit" value="submit your guess" /></p>
</form>
</body>
</html>
<?php
$num_to_guess = 42;
$num_tries = (isset($_POST["num_tries"])) ? $_POST["num_tries"] + 1 : 1;
if (!isset($_POST["guess"])) {
$message = "Welcome to the guessing machine!";
} else if ($_POST["guess"] > $num_to_guess) {
$message = $_POST["guess"]." is too big! Try a smaller number.";
} else if ($_POST["guess"] < $num_to_guess) {
$message = $_POST["guess"]." is too small! Try a larger number.";
} else { // must be equivalent
$message = "Well done!";
}
$guess = $_POST["guess"];
?>
<html>
<head>
<title> Saving state with a hidden field</title>
</head>
<body>
<h1><?php echo $message; ?></h1>
<p><strong>Guess number:</strong> <?php echo $num_tries; ?></p>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST">
<p><strong>Type your guess here:</strong>
<input type="text" name="guess" value="<?php echo $guess; ?>" />
<input type="hidden" name="num_tries" value="<?php echo $num_tries; ?>" />
<p><input type="submit" value="submit your guess" /></p>
</form>
</body>
</html>
相关文章推荐
- LAMP源码环境搭建WEB服务器Linux+Apache+MySQL+PHP
- 实战 LAMP 服务器配置 完整篇
- 64位CentOS 6.0下搭建LAMP环境详细步骤
- centos6.5 编译安装lamp以及相关错误的解决方法
- CentOS 6.3编译安装LAMP环境笔记
- linux 安装配置lamp v2
- CentOS 6.3下源码安装LAMP(Linux+Apache+Mysql+Php)运行环境步骤
- 调整优化您的LAMP应用程序的5种简单方法
- linux服务器系统中手动配置lamp服务器的经验分享
- LAMP&LNMP自动化安装脚本代码
- 阿里云主机一键安装lamp、lnmp环境的shell脚本分享
- CentOS 6.4安装配置LAMP服务器(Apache+PHP5+MySQL)
- LAMP环境配置
- LAMP 源代码包安装异常记录(64位)
- 阿里云Ubuntu系统从0搭建LAMP(Apache+MySQL+PHP)以及服务器生产环境常用配置
- Linux下搭建LAMP环境
- LAMP
- LAMP 架构服务器性能优化建议(转)
- lamp源码安装
- LAMP架站相关问题:phpmyadmin 无法载入 mcrypt 扩展, 请检查 PHP 配置