Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2008
    Location
    malaysia
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy Saving state with a hidden field

    Hi, I am learning PHP from sams each yourself php, mysql and apache.
    Need help with the following exercise, saving state with a hidden field.

    It is a simple script of "number guessing machine". The problem is that the "guess number" does not increased with each guess!

    The script is as follow:

    <?php
    $num_to_guess =42;
    $num_tries = (isset($_POST[num_tries]))? $num_tries + 1: 0 ;
    $message = "";
    if(!isset($_POST[guess])){
    $message = "welcome to the guessing game.";
    }elseif ($_POST[guess] > $num_to_guess){
    $message = "$_POST[guess] Wrong, it s too big.";
    }elseif ($_POST[guess] < $num_to_guess){
    $message = "$_POST[guess] Wrong, it is too small";
    }else{
    $message ="$_POST[guess] Correct guess!";

    }
    $guess = $_POST[guess];
    ?>
    <html>
    <head>
    <title> </title>
    </head>
    <body>
    <h1>
    <?php print $message ?>
    </h1>
    Guess Number: <?php print $num_tries ?>
    <form action="<?php print $_SERVER[PHP_SELF] ?>" method="post">
    Type your guess here:
    <input type="text" name="guess" value="<?php print $guess ?>">
    <input type="hidden" name="num_tries" value="<?php print $num_tries ?>">
    <input type="submit" value="Hit it">
    </form>
    </body>
    </html>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Your code is not assigning the value there
    Code:
    $num_tries = (isset($_POST[num_tries]))? $num_tries + 1: 0 ;
    should be something
    Code:
    $num_tries = (isset($_POST['num_tries']))? $_POST['num_tries'] + 1: 0 ;
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    theriver (01-22-2008)

  • #3
    New to the CF scene
    Join Date
    Jan 2008
    Location
    malaysia
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    Your code is not assigning the value there
    Code:
    $num_tries = (isset($_POST[num_tries]))? $num_tries + 1: 0 ;
    should be something
    Code:
    $num_tries = (isset($_POST['num_tries']))? $_POST['num_tries'] + 1: 0 ;
    Thank you, the script is working now with your help.
    Could you explain a little bit the purpose of the following code

    <input type="hidden" name="num_tries" value="<?php print $num_tries ?>">

    Thanks again

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    It simply storing the current value of trial in a hidden field to be used for the next trial.
    Convince yourself by changing hidden to text
    (Otherwise you may have to use a SESSION variable to keep it in memory for later use.)
    Last edited by abduraooft; 01-22-2008 at 01:25 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    theriver (01-23-2008)

  • #5
    New to the CF scene
    Join Date
    Jan 2008
    Location
    malaysia
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Thumbs up

    Quote Originally Posted by abduraooft View Post
    It simply storing the current value of trial in a hidden field to be used for the next trial.
    Convince yourself by changing hidden to text
    (Otherwise you may have to use a SESSION variable to keep it in memory for later use.)
    Thanks again for your useful tip.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •