- Use a hidden field to keep track of this value.
- A hidden field behaves the same as a text field, except that the user cannot see it unless he views the HTML source of the document that contains it.
Saving State with a Hidden Field
Math1.php
<body>
<h1> First Number</h1>
<form action="math2.php" method="post">
<label for= "fn"> First Number </label>
<input type="text" name="fn" value="" id="fn" >
<p> <input type="submit" value="continue →"></p>
</form>>
</body>
Math2.php
<body>
<h1> Second Number</h1>
<form action="math3.php" method="post">
<input type="hidden" name="fn" value=" <?php echo $_POST['fn'] ?> " id="fn" >
<label for= "sn"> Second Number </label>
<input type="text" name="sn" value="" id="sn" >
<p> <input type="submit" value="continue →"></p>
</form>>
</body>
Math3.php
<?php
$ans = $_POST['fn'] + $_POST['sn'] ;
?>
<body>
<h1> Answer is .....</h1>
<?php
echo "The Answer is $ans";
?>
</body>
Output:-
<?php
$num_to_guess = 42;
$message = "";
if (! isset ( $_POST ['guess'] )) {
$message = "Welcome!";
} else if ($_POST ['guess'] > $num_to_guess) {
$message = $_POST ['guess'] . " is too big!";
} else if ($_POST ['guess'] < $num_to_guess) {
$message = $_POST ['guess'] . " is too small!";
} else {
$message = "Well done!";
}
$guess = ( int ) $_POST ['guess'];
$num_tries = ( int ) $_POST ['num_tries'];
$num_tries ++;
?>
<html>
<head>
<title>A PHP Number Guessing Script</title>
</head>
<body>
<?php print $message?>
Guess number: <?php print $num_tries?><br />
<form method="post" action="<?php print $_SERVER ['PHP_SELF']?>">
<input type="hidden" name="num_tries" value="<?php print $num_tries?>" />
Type your guess here:
<input type="text" name="guess" value="<?php print $guess?>" />
</form>
</body>
</html>
0 Comments