Page 1 of 1

Undefined variable.

Posted: Tue Jun 04, 2002 12:46 pm
by Nacx
I got a problem with this code, which should take take a username and a comment from two forms in a html doc, and place the text in a file.

<form action="FormHandler.php" method="POST">
<INPUT type="text" NAME="username" size="30"> <BR>
<TEXTAREA NAME="comment" ROWS=6 COLS=60></TEXTAREA><br>
<input type="submit" value="Send form">
<INPUT TYPE=reset VALUE="Clear">
</form>

<?php
function write() {
$filename = "wall.txt";
$info = $username && "<br>" && $comment;
$line = "<br>----------------------------------------------------<br>";
$openfile = fopen("$filename", "a");
$writetext = $line && $info;
fwrite($openfile, $writetext);
fclose($openfile);
}
write();
?>

But $username is undefined, it says. Can anybody help me out here? If you see another error somewhere, please notice me about that too.
Thanks.

Posted: Tue Jun 04, 2002 12:52 pm
by twigletmac

Posted: Tue Jun 04, 2002 1:23 pm
by Nacx
I've fixed the variable problem now. But I have a new problem. Instead of writing the text of the forms into the file, it writes only: 1
Whats wrong with the rest of the code then?

Posted: Tue Jun 04, 2002 2:10 pm
by twigletmac
Try

Code: Select all

<form action="FormHandler.php" method="post"> 
<input type="text" name="username" size="30" /><br /> 
<textarea name="comment" rows="6" cols="60"></textarea><br /> 
<input type="submit" value="Send form" /> 
<input type=reset value="Clear" /> 
</form>

<?php 
function write() &#123; 
$filename = 'wall.txt'; 
$info = $_POST&#1111;'username']."\n".$_POST&#1111;'comment']."\n"; 
$line = "\n----------------------------------------------------\n"; 
$openfile = fopen($filename, 'a'); 
$writetext = $line.$info; 
fwrite($openfile, $writetext); 
fclose($openfile); 
&#125; 
write(); 
?>
Mac

Posted: Wed Jun 05, 2002 4:30 am
by MattF
Shouldn't there be a line like this:

Code: Select all

<?PHP
if(isset($_POST&#1111;'username']) && isset($_POST&#1111;'comment']))
?>
Otherwise it will just write blank things when they just view the form.

Posted: Wed Jun 05, 2002 4:39 am
by twigletmac
Good point. I think I was working under the assumption that the PHP was on a different page to the form itself. Should probably have that line in there just in case anything else has gone screwy though.

Mac