Click to See Complete Forum and Search --> : What's wrong with this code?
gokou
04-29-2003, 02:03 PM
This is a practice thing in a book. I copy it exactly now it says in the book, but I always get an error where the if statement is. Also, it tells me to guess a number and when I do, something is suppose to print, but nothing does. It's very annoying and I would hate to move on in the book without figuring this out.
I get this error:
Notice: Use of undefined constant guess - assumed 'guess' in c:\apache group\apache\htdocs\php_testing\listing9.7.php on line 4
Heres the code:
<?php
$num_to_guess = 42;
$message = "";
if (!isset($_post[guess])) {
$message = "Welcome to the guessing machine!";
} 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 { // must be equivalent
$message = "Well done!";
}
?>
<html>
<head>
<title>Listing 9.7 A PHP number guessing script</title>
</head>
<body>
<h1>
<?php print $message ?>
</h1>
<form action="<?php print $_SERVER[PHP_SELF] ?>" method="post">
Type your guess here:
<input type="text" name="guess">
<input type="hidden" name="num_tries" value="<?php print $num_tries?>">
</form>
</body>
</html>
Try putting quotes around it, like this:
if (!isset($_post["guess"])) {
and do the same for all in your script, including $_SERVER[PHP_SELF] (should be $_SERVER["PHP_SELF"] )
gokou
04-29-2003, 09:30 PM
ahh Thanks! I didn't think of that. That solves all the errors for all the scripts I had that I was having trouble with. Normally I would always put quotes around words, but in the book it didn't show it so I didn't.
Still I don't understand how this script is suppose to work. I think the text is suppose to change where it says "welcome to the guessing machine" when you type a number in the text box. That never happens though and usually you would have to hit some sort of submit button(least what i'm use too).
Are you sure this is the complete script from the book, because it is seriously broken... ie, you don't have any variable named $num_tries, yet it is trying to write that to a hidden field, etc...
gokou
04-30-2003, 01:24 AM
oops. I looked through it and I had listing 9.7 and 9.8 mixed in with each other. I fixed it. There is still one error, but it works right. The error is on line 14, where the $guess variable is first created.
<?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!";
} 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 { // must be equivalent
$message = "Well done!";
}
$guess = $_POST['guess'];
?>
<html>
<head>
<title>Listing 9.8 Saving state with a hidden field</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?>">
</form>
</body>
</html>
Nevermind. There seems to be alot of errors still.
It's probably your server, as the code looks good to me, and works fine: http://www.infinitypages.com/temp/gokou.php is the exact code that you posted.
gokou
04-30-2003, 12:46 PM
I was hoping it wouldn't be something like that because I don't know to fix it. Would it tell me how in a apache book? I have 3 of them, I just haven't read them yet.
Hmm... Not sure. It might tell you what you need to change. I can't see anything in that code that shouldn't have worked in a default install, though...
gokou
04-30-2003, 02:38 PM
There are the kind of error I get. Theres always a problem with the variables.
Notice: Undefined variable: num_tries in c:\apache group\apache\htdocs\php_testing\listing9.8.php on line 3
Notice: Undefined index: 'guess' in c:\apache group\apache\htdocs\php_testing\listing9.8.php on line 10
gokou
05-01-2003, 12:50 AM
My server is all messed up. I'm only followiing the instructions in the book so it should be working. I never know what the problem is.
I'm trying to make a script that submits data to an email. When you hit submit. it brings up an internal error page and says on the bottom "Apache/1.3.27 Server at jason.socal.rr.com Port 80". I looked in the error log and it says "Premature end of script headers". I am on a lan, maybe that has something to do with it. It it affects my ftp anyways.
Heres the html part:
<html>
<head>
<title>E-Mail Form</title>
</head>
<body>
<form action="listing9.12.php" method="post">
Your Name: <input type="text" name="name"><br><br>
Your E-Mail Address: <input type="text" name="email"><br><br>
Message:<br>
<textarea name="message" cols=30 rows=5></textarea><br><br>
<input type="submit" value="Send Form">
</form>
</body>
</html>
Heres the php part:
<html>
<head>
<title>Listing 9.12 Sending mail from the form in Listing 9.11</title>
</head>
<body>
<?php
print "Thank you, <b>$_POST['name']</b>, for your message!<br><br>\n\n";
print "Your e-mail address is: <b>$_POST['email']</b><br><br>\n\n";
print "Your message was:<br><br>\n\n";
print "$_POST['message'] <br><br>";
//Start building the mail string
$msg = "Name: $_POST['name']\n";
$msg .= "E-Mail: $_POST['email']\n";
$msg .= "Message: $_POST['message']\n";
//set up the mail
$recipient = "jasonjitsu@hotmail.com";
$subject = "Form Submission Results";
$mailheaders ="From: My Web Site <defaultaddress@yourdomain.com> \n";
$mailheaders .= "Reply-To: $_POST['email']";
//send the mail
mail($recipient, $subject, $msg, $mailheaders);
?>
</body>
</html>
Try this, as your PHP code:
<html>
<head>
<title>Listing 9.12 Sending mail from the form in Listing 9.11</title>
</head>
<body>
<?php
print "Thank you, <b>".$_POST['name']."</b>, for your message!<br><br>\n\n";
print "Your e-mail address is: <b>".$_POST['email']."</b><br><br>\n\n";
print "Your message was:<br><br>\n\n";
print $_POST['message']."<br><br>";
//Start building the mail string
$msg = "Name: ".$_POST['name']."\n";
$msg .= "E-Mail: ".$_POST['email']."\n";
$msg .= "Message: ".$_POST['message']."\n";
//set up the mail
$recipient = "ryan@infinitypages.com";
$subject = "Form Submission Results";
$mailheaders ="From: My Web Site <defaultaddress@yourdomain.com> \n";
$mailheaders .= "Reply-To: ".$_POST['email'];
//send the mail
mail($recipient, $subject, $msg, $mailheaders);
?>
</body>
</html>
The difference between my code and yours is that in mine, around the PHP variables, I added ". var ." so they would get read....
gokou
05-01-2003, 12:10 PM
I made a big mistake. I was looking for listing9.12 in htdocs to try out your code. When I found it, it was saved as listing9.12.txt. Obviously that would cause some problems'. So I fixed that and when I tryed it, it sent, but all of the posting variables had errors' because of the single quatation marks'. So I deleted those and it works fine. It's weird, on the last code I had problems on, it needed those quatation marks to work, but this one wouldn't work with them there.
Thanks for posting your code. It still helped.