0

I'm going through SAMS Teach Yourself PHP, MYSQL, and Apache all in one and I can't seem to get this example to work, which is used again in the next lesson. I usually type them in by hand as this one, but I've also tried cutting and pasting and removing the line numbers directly from the online tutorial, and still can't get anything but a blank page.

Thanks in advance for any help!

<?php
$num_to_guess = 42;

if(!isset($_POST[guess]])) {
	$message = "WElcome to the guessing machine!";
	}
	else if($_POST[guess] > $num_to_guess) {
	$message = "$_POST[guess] is too big!  Try a smaller number.";
	}
	else if($_POST[guess] < $num_to_guess) {
	$message = "$_POST[guess] is too small!  Try a larger number.";
	}
	else {
	$message = "Well Done!"; 
	}
?>
	
<html>
<head>
<title> A PHP number guessing script</title>
</head>
<body>
<h1><?php echo $message; ?></h1>
<form action="<?php echo $_SERVER[PHP_SELF]; ?>" method="POST">
<p><strong>Type your guess here:</strong><input type="text" name="guess"></p>
<p><input type="submit" value="submit your guess"></p>
</form>
</body>
</html>
3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by MWE_QUE
0

Hi MWE_QUE,

Was that code supposed to work as it came as there are a number of errors in it (mainly quotation marks)?

Try . . .

<?php
$num_to_guess = "42";

if(!isset($_POST['guess'])) {
	$message = "Welcome to the guessing machine!";
}
else if($_POST['guess'] > $num_to_guess) {
	$message = "$_POST[guess] is too big!  Try a smaller number.";
}
else if($_POST['guess'] < $num_to_guess) {
	$message = "$_POST[guess] is too small!  Try a larger number.";
}
else {
	$message = "Well Done!";
}
?>

<html>
<head>
<title> A PHP number guessing script</title>
</head>
<body>
<h1><?php echo $message; ?></h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><strong>Type your guess here:</strong><input type="text" name="guess"></p>
<p><input type="submit" value="submit your guess"></p>
</form>
</body>
</html>

Hope this helps.
Zagga

0

Yes the code is supposed to work, and it all has so far. I'm about 59 modules in out of 200+, but that includes installation and everything. I see the single quotes, and went back through a couple of lessons to see if I was missing something I'm still getting a blank page with the improvements made.

I looked at "Sam's" sample again and there weren't any single quotes. The WElcome on line 5 was my typo.

Thanks for the help

0

There was also an extra closing square bracket on line 4

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

As far as quotes are concerned, the PHP manual says

The simplest way to specify a string is to enclose it in single quotes (the character ').

The code I posted works in my browser. Are you getting nothing at all?


Zagga

0

Ok it works now. I had to sleep for a while. It took me some time to find the location of my error log. And it showed error on line 5 which was actually the bracket mentioned. I copied and pasted your code, and it works fine (I had missed the bracket correction when I edited my code).

I appreciate all the help. Thank you.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.