Jump to content

Simple Help Question


Alanistic

Recommended Posts

Hi All,

 

I've just started teaching myself PHP using "PHP 5 In Easy Steps" by Mike McGrath, and one of the php scripts isn't running as expected.  I've gone through the code several times and as far as I can see everything is correct. 

 

The code prompts for a number between 1 and 20, and should either display too high, too low, or invalid.  Unfortunately, it seems to be getting stuck at the invalid bit and no matter what number I specify it always says invalid entry.  Is there anything obvious that I've done wrong?

 

 

 

<?php header ( "Cache-Control:no-cache" );

 

/* Use cache control to ensure that the page is reloaded with each visit

  and is not re-loaded from the browsers cache.  */

 

 

 

/* setnum function to randomly generate a random number between 1 and 20

  and set this to the $num variable.  */

 

function setnum() {

global $num;

srand( (double)microtime() * 100000 );

$num = rand( 1, 20 );

}

 

?>

 

<html> <head> <title> Number Guess </title> </head>

<body>

 

 

<?php

 

# Assigns values from form.

 

$num = $_POST['num'];

$self = $_SERVER['PHP_SELF'];

 

 

# Displays starting instructions.

 

if( $num == null) {

$msg = "I have thought of a number between 1 and 20";

$msg .= " <h3>guess what it is ...</h3>";

}

 

 

# Error message for non numeric guesses

 

if( $num != null and !is_numeric($guess) ) {

$msg = "Your guess was invalid<h3>Try again!</h3>";

}

 

 

# Check to see if the guess is correct.

 

else if ( $guess == $num ) {

if ( $num != null) {

$msg = "CORRECT!  The number was $num";

$msg .= "<h3><a href = \"$self\" >";

$msg .= "CLICK HERE TO TRY AGAIN???</a></h3>";

}

setnum();

}

 

 

# Check to see if guess is higher than the number.

 

else if ( $guess > $num ) {

$msg = "You guessed $guess<h3>My number s lower!</h3>";

}

 

 

# Check to see if the guess is lower than the number.

 

else if ( $guess < $num ) {

$msg = "You guessed $guess<h3>My number is higher!</h3>";

}

 

 

# Display message.

 

echo ( $msg );

 

?>

 

<!-- Uses HTML code to call $self variable -->

 

<form action = "<?php $self ?>" method = "post">

<input type = "hidden" name = "num" value = "<?php echo( $num ); ?>" >

Guess:<input type = "text" name = "guess">

<input type = "submit" value = "Submit">

 

</form>

</body>

</html>

Link to comment
Share on other sites

If that's the code from the book, i suggest you buy a different one. It's terrible. As for the problem, $guess is undefined. You need to take it out of the $_POST superglobal first:

 

$guess = $_POST['guess'];

 

Thanks for the feedback.  That solved my problem.  1 tiny mistake.

 

You mentioned the code is terrible, what makes it bad code?

Link to comment
Share on other sites

You mentioned the code is terrible, what makes it bad code?

 

Global variables, odd logic, weird HTML comments etc. Dont get me wrong, if this is something you've written yourself as you're learning, then don't worry about that. My point was that it would not be good code to be learning from.

Link to comment
Share on other sites

I'm with GingerRobot on that code being a bad example to be learning from.

 

The setnum() function is using the global keyword to access $num instead of properly being it into the function as a parameter and return it as a result.

 

The php code in the <form action="...." parameter is not actually echoing the variable $self. The form only works because an empty "" action value causes the form to submit to the same page.

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.