PHP - How can I store variable states?

Jacob

I'm learning php, and I'm having a hard time finding answers to this. How do I keep a variable after I've submitted a form that has to call the page again? (Weird wording?) I am trying to keep $num_to_guess and $num_tries. I'm following instructions I found to include $num_tries as a hidden input in the html block, but it still does not increment it after I submit the form.

How do I save $num_to_guess AND $num_tries after submission?


<?php
  if ( !isset ( $num_tries ) )
  {
    $num_to_guess = rand(1, 100);
  }
  $num_tries = ( isset ( $_POST['num_tries'] ) ) ? $num_tries + 1 : 0;
  if ( !isset ( $_POST['guess'] ) ) {
    $message = "Welcome to the guessing machine!";
  } elseif ( !is_numeric ( $_POST['guess'] ) ) {
    $message = "I don't understand that response...";
  } elseif ( $_POST['guess'] == $num_to_guess ) {
    $message = "Well done! That is the number!";
  } elseif ( $_POST['guess'] < $num_to_guess ) {
    $message = $_POST['guess'] . " is too small!";
  } elseif ( $_POST['guess'] > $num_to_guess ) {
    $message = $_POST['guess'] . " is too big!";
  } else {
    $message = "I am terribly confused...";
  }
?>

<!DOCTYPE html>
<html>
<head>
  <title>A PHP Number Guessing Script</title>
</head>

<body>
  <h2>The Guessing Game</h2>
  <p>1-100<br/>
    Guess number: <?php echo $num_tries ?></p>
  <h3><?php echo $message; echo $num_to_guess?></h3>
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <p><label for="guess">Type your guess here:</label><br/>
      <input type="text" id="guess" name="guess" /></p>
    <input type="hidden" name="num_tries" value="<?php echo $num_tries; ?>"/>
    <button type="submit" name="submit" value="submit">Submit</button>
  </form>
</body>
</html>
VIDesignz

You will want to set these to a session variable like this

session_start();
$_SESSION['guess'] = $num_to_guess;
$_SESSION['tries'] = $num_tries;

Then to access them you would do...

session_start();
$guess = $_SESSION['guess'];
$tries = $_SESSION['tries'];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I store the output of summary in a variable?

From Dev

How can I store a knex query in a variable?

From Dev

How can I store dataset() in Application variable?

From Dev

How can I store my variable in a file?

From Dev

How can I store the result of a function into a variable?

From Dev

Can I Store a Single Variable (not in a Table) In PHP/MySQL Database

From Dev

can i store a cookie value set from javascript into a php variable?

From Dev

How can I write a query with 2 states?

From Dev

How can store javascript variable to php session variables?

From Dev

How can I store a variable length array in a struct in C

From Dev

How can I store a method in a variable in Java 8?

From Dev

How can I store string from object as a variable?

From Dev

How can I store a result of a function in a variable in Python?

From Dev

How can I store an object in MeteorJS/React in a variable to return later?

From Dev

How can I store the value a user enters in a variable?

From Dev

How can I store the value of a MySqlCommand in C# to a local variable?

From Dev

How can I store a string literal to a variable with no substitution or interpretation?

From Dev

How can I store a result of a function in a variable in Python?

From Dev

how can i store the printing result, obtained by "for statement" variable?

From Dev

How can I store a permanent variable on my website?

From Dev

How can I store output string to a variable AND display in console

From Dev

How i can destroy an GameObject but also store the gameobject in a variable

From Dev

How do i store select value into a php variable

From Dev

How do I store the number of rows affected in a variable using php?

From Dev

How can i change a php variable with a button

From Dev

PHP How can I store variables in an array and call them

From Dev

How can i store and retrieve validated form data in PHP?

From Dev

PHP How can I store variables in an array and call them

From Dev

How I can put a variable into variable in PHP OOP?

Related Related

  1. 1

    How can I store the output of summary in a variable?

  2. 2

    How can I store a knex query in a variable?

  3. 3

    How can I store dataset() in Application variable?

  4. 4

    How can I store my variable in a file?

  5. 5

    How can I store the result of a function into a variable?

  6. 6

    Can I Store a Single Variable (not in a Table) In PHP/MySQL Database

  7. 7

    can i store a cookie value set from javascript into a php variable?

  8. 8

    How can I write a query with 2 states?

  9. 9

    How can store javascript variable to php session variables?

  10. 10

    How can I store a variable length array in a struct in C

  11. 11

    How can I store a method in a variable in Java 8?

  12. 12

    How can I store string from object as a variable?

  13. 13

    How can I store a result of a function in a variable in Python?

  14. 14

    How can I store an object in MeteorJS/React in a variable to return later?

  15. 15

    How can I store the value a user enters in a variable?

  16. 16

    How can I store the value of a MySqlCommand in C# to a local variable?

  17. 17

    How can I store a string literal to a variable with no substitution or interpretation?

  18. 18

    How can I store a result of a function in a variable in Python?

  19. 19

    how can i store the printing result, obtained by "for statement" variable?

  20. 20

    How can I store a permanent variable on my website?

  21. 21

    How can I store output string to a variable AND display in console

  22. 22

    How i can destroy an GameObject but also store the gameobject in a variable

  23. 23

    How do i store select value into a php variable

  24. 24

    How do I store the number of rows affected in a variable using php?

  25. 25

    How can i change a php variable with a button

  26. 26

    PHP How can I store variables in an array and call them

  27. 27

    How can i store and retrieve validated form data in PHP?

  28. 28

    PHP How can I store variables in an array and call them

  29. 29

    How I can put a variable into variable in PHP OOP?

HotTag

Archive