To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Upgrading PHP

Upgrading PHP Issues concerning PHP version upgrades and future releases

Reply
 
Thread Tools Rate Thread Display Modes
Old 09-03-2004, 05:50 AM   #1
Mr66
Junior Member
 
Join Date: Sep 2004
Location: Taipei, Taiwan
Posts: 2
PHP 5 on IIS 5 question

Hello, I'm learning PHP from a book teaching PHP 4.3 and I've ran into a problem I can't solve.

<?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 machine!";
}
else if ($_POST['guess'] > $num_to_guess)
{
$message = $_POST['guess'] . " is to big! Try a smaller number";
}
else if ($_POST['guess'] < $num_to_guess)
{
$message = $_POST['guess'] . " is too small! Try a larger number";
}
else
{ // must be equivalent
$message = "Well done! " . $_POST['guess'] . " is correct";
}

$guess = $_POST['guess'];

?>

<html>
<head>
<title>
Listing 9.6 A PHP number guessing script
</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><strong>Type your guess here:</strong>
<input type="text" name="guess" value="<?php echo $guess ?>">
<input type="hidden" name="num_tries" value="<?php echo $num_tries ?>">
</p>
<p><input type="submit" value="submit your guess"></p>
</form>
</body>
</html>


The error I receive when I open the page
PHP Notice: Undefined index: guess in c:\inetpub\wwwroot\php\listing9.7.php on line 25

I know if has something to do with the <?php echo $guess ?> and <?php echo $num_tries ?> in the html part. But can't figure it out. Anyone have any ideas what I'm doing wrong.
Mr66 is offline   Reply With Quote
Old 09-03-2004, 07:04 AM   #2
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,126
Sorry; is this a PHP5 question or a PHP4.3 question?

PHP Code:
if (!isset($_POST['guess']))
{
    
$message = "Welcome to the guessing machine!";
}
else if (
$_POST['guess'] > $num_to_guess)
{
    
$message = $_POST['guess'] . " is to big! Try a smaller number";
}
else if (
$_POST['guess'] < $num_to_guess)
{
    
$message = $_POST['guess'] . " is too small! Try a larger number";
}
else
{
// must be equivalent
    
$message = "Well done! " . $_POST['guess'] . " is correct";
}

$guess = $_POST['guess'];
If $_POST['guess'] is not set (i.e., that field was empty); then the message will be set to "Welcome to the guessing machine!".

The problem is, even if it wasn't set, the code still tries to use it later on (the line $guess=$_POST['guess']; ). That's when the error will happen. Considering that $guess is used later to repopulate the form field, a reasonable fix would be to add a line immediately after the "Welcome to the guessing machine!" line to make sure $_POST['guess'] is set with an empty value:
PHP Code:
$_POST['guess']="";
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 09-03-2004, 12:30 PM   #3
Mr66
Junior Member
 
Join Date: Sep 2004
Location: Taipei, Taiwan
Posts: 2
I'm using PHP 5

I'm using PHP 5. That worked to clear up the initial problem but once you enter info into the text field and click submit

then the other one is called variable is called and it produces a similiar error. But I would think that it would have

already triggered an error at the beginning of the file.

The error I get now is:
PHP Notice: Undefined variable: num_tries in c:\inetpub\wwwroot\php\listing9.7.php on line 4

This is how the new file looks after I've added your change. I'm guessing that I need to do the same thing only figure out

exactly were to put the num_tries. I thought that it set it at the top of the script. Also, thanks for the input. Do you

know if this was just bad coding or something different from PHP 4 to PHP 5, and if so, do you know where it might be

covered in the php manual. Haha, sorry to ask so much. Well again thanks for the help.

<?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 machine!";
$_POST['guess']="";

}
else if ($_POST['guess'] > $num_to_guess)
{
$message = $_POST['guess'] . " is to big! Try a smaller number";
}
else if ($_POST['guess'] < $num_to_guess)
{
$message = $_POST['guess'] . " is too small! Try a larger number";
}
else
{ // must be equivalent
$message = "Well done! " . $_POST['guess'] . " is correct";
}

$guess = $_POST['guess'];

?>

<html>
<head>
<title>
Listing 9.6 A PHP number guessing script
</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><strong>Type your guess here:</strong>
<input type="text" name="guess" value="<?php echo $guess ?>">
<input type="hidden" name="num_tries" value="<?php echo $num_tries ?>">
</p>
<p><input type="submit" value="submit your guess"></p>
</form>
</body>
</html>
Mr66 is offline   Reply With Quote
Old 09-17-2004, 06:45 AM   #4
kkobashi
Senior Member
 
kkobashi's Avatar
 
Join Date: Aug 2004
Location: Silicon Valley, CA
Posts: 273
Ask this in the coding forum... you'll get more eyeballs there than here.
__________________
Kerry Kobashi
Kobashi Computing
kkobashi is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 09:55 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.