4 Replies - 284 Views - Last Post: 10 December 2017 - 11:28 AM Rate Topic: -----

#1 Nonsense7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-October 14

Undefined Variable?

Posted 10 December 2017 - 09:10 AM

Getting an undefined variable error on line 3, I don't understand why?
<?php
$num_to_guess = 42;
$num_tries = (isset($_POST['num_tries'])) ? $num_tries + 1 : 1;
if (!isset($_POST['guess'])) {
    $message = "Welcome to the guessing machine!";
} elseif (!is_numeric($_POST['guess'])) {
    //is not numeric
    $message = "I Don't understand that response.";
} elseif ($_POST['guess'] == $num_to_guess) {
    //mathces!
    $message = "Well Done!";
} elseif ($_POST['guess'] > $num_to_guess) {
    $message = $_POST['guess']." is too big! try a smaller number.";
} elseif ($_POST['guess'] < $num_to_guess) {
    $message = $_POST['guess']." is too small! Try a larger number.";
} else {//some other condition
    $message = "I am terribly confused.";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>An HTML form that calls itself</title>    
</head>
<body>
    <h1><?php echo $message; ?></h1>
    <p><strong>Guess number:</strong> <?php echo $num_tries; ?></p>
    <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="sumbit">Send Message</button>
    </form>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Undefined Variable?

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13656
  • View blog
  • Posts: 54,487
  • Joined: 12-June 08

Re: Undefined Variable?

Posted 10 December 2017 - 09:35 AM

It's been a while since I have been in PHP, but I would think the condition '$num_tries + 1' would pose a problem if $num_tries hasn't been created yet. the line above - make it so $num_tries = 0 and then try it.
Was This Post Helpful? 0
  • +
  • -

#3 Nonsense7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-October 14

Re: Undefined Variable?

Posted 10 December 2017 - 10:01 AM

That stopped the error, but the $num_tries variable won't update. So now is that a separate issue? Every time I enter a number to the "guess" box, its supposed to add one to the $num_tries. I tried using your suggestion then making separate variable that would update from $num_tries, like so (but its still wouldn't update)

<?php
$num_to_guess = 42;
$num_tries = 0;
$guess_tries = (isset($_POST['num_tries'])) ? $num_tries + 1 : 1;
if (!isset($_POST['guess'])) {
    $message = "Welcome to the guessing machine!";
} elseif (!is_numeric($_POST['guess'])) {
    //is not numeric
    $message = "I Don't understand that response.";
} elseif ($_POST['guess'] == $num_to_guess) {
    //mathces!
    $message = "Well Done!";
} elseif ($_POST['guess'] > $num_to_guess) {
    $message = $_POST['guess']." is too big! try a smaller number.";
} elseif ($_POST['guess'] < $num_to_guess) {
    $message = $_POST['guess']." is too small! Try a larger number.";
} else {//some other condition
    $message = "I am terribly confused.";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>An HTML form that calls itself</title>    
</head>
<body>
    <h1><?php echo $message; ?></h1>
    <p><strong>Guess number:</strong> <?php echo $guess_tries; ?></p>
    <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 $guess_tries; ?>" />
        <button type="submit" name="submit" value="sumbit">Send Message</button>
    </form>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13656
  • View blog
  • Posts: 54,487
  • Joined: 12-June 08

Re: Undefined Variable?

Posted 10 December 2017 - 11:02 AM

PHP is a server side language, right? To have the code act the page must go back to the webserver, do what ever, and display it back to the client.

If you have a variable declared on a bit of php code each time the page is called it is like starting it all over again. You should read up on 'sessions'.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6442
  • View blog
  • Posts: 26,058
  • Joined: 12-December 12

Re: Undefined Variable?

Posted 10 December 2017 - 11:28 AM

The OP is posting back the value num_tries each time, which will be increased ;) (once working).

@OP You are resetting the variable $num_tries to zero each time, so $guess_tries will only ever be 1. You need to read and increase the posted value num_tries.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1