Teen Programmers Unite  
 

 

Return to forum top

Simple PHP Form Communication Problem

Posted by 142857 [send private reply] at September 02, 2002, 05:17:46 PM

Hi. I'm starting to learn PHP, and to practice, am working on a simple guestbook. Here is the code for "guestbook.php":

<html><head><title>Guestbook</title></head>
<body>

<?php
$file_loc = "guestmessages.txt"; /*contains the actual messages*/
$file_counter_loc = "messagecounter.txt"; /*contains the # of messages*/

$file = fopen($file_counter_loc, "r");
if(!$file)
{
echo "<p>Error: Unable to open $file_counter_loc.</p>";
}
else /*the file can be accessed*/
{
$num = fgets($file, 50); /*reads 50 chars from the file*/
$num = trim($num); /*trims it*/
echo "<p>There are $num messages in the guestbook! To leave a message, see below.</p>";
fclose($file);
}
/*now to open the file containing the messages*/
$file = fopen($file_loc, "r");
if(!$file)
{
echo "<p>Error: Unable to open $file_loc.</p>";
}

else
{
while(!feof ($file))
{
$line = fgets($file, 1024);
echo $line;
}
}
/*note: writetofile.php writes messages to file, increments counter, sends user to guestbook.php*/
?>

<p><b>Leave a message here...</b></p>

<form name="form" action="writetofile.php" method="POST">
<br>Your name: <input type="text" name="name" size="20">
<br>Your e-mail: <input type="text" name="email" size="20">
<br>Your message:
<textarea name="message"></textarea>

<br><input type ="submit" value="Enter your message.">

</form>
</body></html>

Now here is the code for "writetofile.php":
<?php
/*this is the message formatting file. it gets messages from the input on guestbook and writes it to the file.*/
$file_loc = "guestmessages.txt";
$file_counter_loc = "messagecounter.txt"; /*contains the # of messages*/

$file = fopen($file_counter_loc, "r");
$num = fgets($file, 999);
$num = trim($num);
$num++; /*there is one more message*/
fclose($file);
$file = fopen($file_counter_loc, "w");
fputs($file, $num);
fclose($file);

$new_file = fopen("guestmessages.txt", "a"); /*open for appending*/

/*print "name: $name email: $email message: $message";*/

/*fputs($new_file, "$name (<a href=\"mailto:$email\">$email</a>):");*/

fwrite($new_file, "message: $message");
fclose($new_file);
?>

<html><head><title> </title>
<META http-equiv="refresh" content="1; url=guestbook.php">
</head><body></body></html>

I've looked on the net for stuff about forms and PHP, but I can't see why this won't work. I know the file IO stuff works, but writetofile.php won't recognize the variables ($name, $email which I'll use later, and $message for now) POSTed to it by guestbook.php.

Thank you!

Posted by Psion [send private reply] at September 02, 2002, 05:26:21 PM

Wow. Another post that someone who read the just-added message to new members would not have made. Glad I added it. =)

Please don't post more than 5 lines of code in the future! I don't know PHP, so I can't help you with the actual question.

Posted by 142857 [send private reply] at September 02, 2002, 05:36:01 PM

I'm very sorry! I won't do it again.

I'll summarize: guestbook.php has a form on it that sends messages (using POST) to writetofile.php. writetofile.php then takes some variables from the form and writes the variables to a file. but this isn't working. thanks

Posted by mop [send private reply] at September 02, 2002, 06:30:27 PM

Have you chmoded the files its supposed to write to? That stumped me at first.

Posted by AngelOD [send private reply] at September 03, 2002, 08:58:55 AM

You might not have register_globals activated, so to access your variables (name, email and message), you'd do it like this:

$_POST['name'], $_POST['email'] and $_POST['message']

Or, if you have an older version of PHP (unlikely), then use:

$HTTP_POST_VARS['name'] (etc etc etc)

Posted by 142857 [send private reply] at September 03, 2002, 10:52:00 AM

AngelOD, you're an angel! hahaha. I just put in:

$message = $_POST['message'];
$name = $_POST['name'];
$email = $_POST['email'];

and everything works now. Thank you for your help.

Posted by AngelOD [send private reply] at September 03, 2002, 02:42:18 PM

*bows* You're welcome. :o)

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.