Undefined variable.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nacx
Forum Newbie
Posts: 2
Joined: Tue Jun 04, 2002 12:46 pm

Undefined variable.

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Nacx
Forum Newbie
Posts: 2
Joined: Tue Jun 04, 2002 12:46 pm

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply