idk_php Posted April 17, 2021 Share Posted April 17, 2021 <?php session_start(); $_SESSION["correct_answer"] = rand(1,100); $_SESSION["guess"] = 0; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <h1>Welcome to the Great Number Game!</h1> <h2>I am thinking of a number between 1 and 100</h2> <h2>Take a guess!</h2> <form method="post" action="tryguess.php"> <input type="text" id="guess" name="guess"> <input type="submit" value="submit"> <!-- <a href="trynumber.php"><input type="submit" value="reset"></a> --> </form> </body> </html> + tryguess.php <?php session_start(); // header('Location: trynumber.php'); if (isset($_POST['guess'])) { if ($_POST['guess'] == $_SESSION['correct_answer']) { echo "correct!"; } if ($_POST['guess'] < $_SESSION['correct_answer']) { echo "too low"; } if ($_POST['guess'] > $_SESSION['correct_answer']) { echo "too high"; } } ?> Quote Link to comment Share on other sites More sharing options...
Zane Posted April 17, 2021 Share Posted April 17, 2021 In the tryguess.php file, encase the code into a function. On your trynumber.php script, include the tryguess.php script Put the HTML in this trynumber.php script change the form to POST to itself run the function, included by tryguess.php, within the HTML to output the string you want. Quote Link to comment Share on other sites More sharing options...
idk_php Posted April 17, 2021 Author Share Posted April 17, 2021 I am thinking of using header('Location: trynumber.php'); because that is what our assignment is all about. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 18, 2021 Share Posted April 18, 2021 Ok, save the guess result in a session, then if the session is not empty then display result the problem you'll have is you randomise the number every time the form page is loaded. Ideally one page is better for this project but you could do something like this: Without writing the code i'll try to explain what i mean. Checker on a guess check if the guess is correct, save the result message to a session -- if the number is correct then also set the random number to nothing Form if the random number is empty, then generate a new number, if a result has been saved, then display it. Hope that helps Quote Link to comment Share on other sites More sharing options...
Barand Posted April 18, 2021 Share Posted April 18, 2021 All the seond script has to do is check the posted guess against the stored number and pass the result of that check back to the first script. Something like... session_start(); $resp = $_POST['guess'] <=> $_SESSION['correct_number']; header("Location: trynumber.php?response=$resp"); the first page can then check if a response was returned and what the value is. if $_GET['response'] == -1 then too low else if $_GET['response'] == 1 then too high else if $_GET['response'] == 0 correct Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 18, 2021 Share Posted April 18, 2021 While I like @Barand approach, and YES I know this is a basic script, it would be kinda easy to cheat ☺️ However, it does suit your assignment better. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.