Jump to content

How can I make the tryguess.php appear on the trynumber.php?


idk_php

Recommended Posts

<?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";

    }

}

?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.