php - Congratulation page not showing the variable from the number-guessing game -
i have php script number-guessing game , html script congratulation page. if guess correct, game end , congratulation page open. in php, have variable $prize=1000-100 * $_post['tries'], such if first guess right, player win $1000; if player has second guess, prize $100 less, , on. variable saved in hidden field in php $_post['prize']. hope final prize can printed in congratulation page, didn’t work expected. did wrong in html? guys, maria.
guess.php:
<?php if(isset($_post['number'])) { $num = $_post['number']; } else { $num = rand(1,10); } if(isset($_post['prize'])) { $prize =1000-100 * $_post['tries']; } else { $prize = 900; } $tries=(isset($_post['guess'])) ? $_post['tries']+1: 0; if (!isset($_post['guess'])) { $message="welcome guessing game!"; } elseif (!is_numeric($_post['guess'])) { $message="you need type in number."; } elseif ($_post['guess']==$num) { header("location: congrats.html"); exit; } elseif ($_post['guess']>$num) { $message="try smaller number"; } else { $message="try bigger number"; } ?> <!doctype html> <html> <head> <title>guessing game</title> </head> <body> <h1><?php echo $message; ?></h1> <p><strong>guess number: </strong><?php echo $tries; ?></p> <form action="<?php echo $_server['php_self']; ?>" method="post"> <p><label for="guess">type guess here:</label><br/> <input type="text" id="guess" name="guess" /> <input type="hidden" name="tries" value="<?php echo $tries; ?>"/><br/> <input type="hidden" name="number" value="<?php echo $num; ?>"/><br/> <input type="hidden" name="prize" value="<?php echo $prize; ?>"/> </p> <button type="submit" name="submit" value="submit">submit</button> </form> </body> </html>
congrats.html:
<! doctype html> <html> <header> <title>congratulation!</title> <body>congratulation!<br/> won <?php echo $_post['prize']; ?> dollars! </body> </header> </html>
you need pass value congrats page, using either request or session. i'd recommend using session people cannot alter prize value.
just amend part here:
} elseif ($_post['guess']==$num) { $_session['prize'] = $_post['prize']; header("location: congrats.php"); exit; }
then (you need change congrats page php page use session btw enable php)
congrats.php
<! doctype html> <html> <header> <title>congratulation!</title> <body>congratulation!<br/> won <?php echo $_session['prize']; ?> dollars! </body> </header> </html>
ps: session require session_start() @ top of both documents.
Comments
Post a Comment