PHP表单在提交时重新加载,但重置变量

时间:2017-12-09 18:48:58

标签: php html forms

我的任务是创造一个猜谜游戏。生成随机数,用户试图猜测它。它告诉您是否应该尝试更大或更小的数字,并最终用户猜出正确的数字。

<?php
session_start();

if(empty($_SESSION['num_to_guess'])) { $_SESSION['num_to_guess'] = mt_rand(1,99); }

if (!isset($_POST['guess'])) {
    $message = "Welcome to the Odd Number Guessing Game!";
}

elseif (!is_numeric($_POST['guess'])) { //is not numeric
$message = "I don't understand that response. Please enter a number.";
}

elseif ($_POST['guess'] == $_SESSION['num_to_guess']) { //matches
    $message = "Well done! You've found the secret number. Now try to guess another.";
    $_SESSION['num_to_guess'] = mt_rand(1,99);
}

elseif ($_POST['guess'] < $_SESSION['num_to_guess']) { //greater
    $message = "Try a larger number!";
}

elseif ($_POST['guess'] > $_SESSION['num_to_guess']) { //lesser
    $message = "Try a smaller number!";
}

else {
    $message = "I'm terribly confused.";
}
?>

<!DOCTYPE html> 
<html>

<head>
<title>Odd Number Guessing Machine!</title>
</head>

<body>
<h1><?php echo $message; ?>

<?php echo $_SESSION['num_to_guess']; ?>

</h1>
<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST">

<p><label for="guess">Try to guess an odd number between 1 and 99:</label> <br/>
<input type="text" id="guess" name="guess" /></p>
<button type="submit" name="submit" value="submit">Submit</button>

</form>

</body>

</html>

我的问题是每次用户提交数据时表单都会重新加载,从而更改随机数。我确定有一种解决方法,但我似乎无法找到它。

我想在猜到正确的数字时生成一个新号码。我尝试在elseif循环上添加rand()函数,其中数字被正确猜出,但这并不起作用。

请帮忙。 :)

1 个答案:

答案 0 :(得分:-1)

现在兼容浏览器

 <?php
 session_start();

 $url_of_page = $_SERVER['REQUEST_URI'];

 if(!isset($_SESSION['correct_number'])){ 
 // Create random number only when the previous one has been correctly guessed

        $number_to_guess = rand(0,9);

        $_SESSION['correct_number'] = $number_to_guess;

     }

 if(isset($_SESSION['correct_number'])){

         $num_to_guess = $_SESSION['correct_number']; 
  }

 if(isset($_POST['guess'])){

     $guessed_number = trim($_POST['guess']);
     $reg_exp = '/^([0-9]+)$/';
     $reg_exp_match = preg_match($reg_exp,$guessed_number);

 }

 if(isset($_POST['submit'])){

        if(isset($reg_exp_match)){

            if(!$reg_exp_match){

                 $message = "Enter a number";

             }else{

             if($guessed_number < $num_to_guess){ 
                 // is small

                 $message = "Try a larger number!";

                 }elseif($guessed_number > $num_to_guess){ 
                 // is large

                 $message = "Try a smaller number!";

                 }elseif($guessed_number == $num_to_guess){ 
                 //matches

                 $message = "Well done...You got it";
                 $new_game = TRUE;

                 unset($_SESSION['correct_number']);
                 // A new number can now be created                      
                 }

             }

        }

    }
 ?>

 <!DOCTYPE html> 
 <html>

 <head>
 <title>Odd Number Guessing Machine!</title>
 </head>

 <body>

 <h3>Welcome to the Guessing Machine!</h3>

 <p>
 <?php 
 if(isset($new_game) && ($new_game == TRUE)){ // The correct number

        if(isset($message) && isset($url_of_page)){ 

             echo $message . '. <a href="' . $url_of_page . '">Start a new game</a>'; 

        }

     }else{ // Not the correct number

         if(isset($message)){ echo $message; } 

}
 ?>
 </p>

 <form action="<?php if(isset($url_of_page)){ echo $url_of_page; } ?>" method="POST">

     <p>
        <label for="guess">Enter a number, 0 to 9:</label> <br/>

        <input type="text" id="guess" name="guess" />

     </p>

     <button type="submit" name="submit" value="submit">Submit</button>

 </form>

 </body>

 </html>