您的位置:首页 > 其它

使用隐藏字段来保存状态

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lamp