Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Guestbook not working...

Guestbook not working...
Thread Tools
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Feb 7, 2004, 02:43 PM
 
Hi guys,
I'm trying to write a guestbook for a site I'm making, and it should write to a text file, I get no parse errors so I don't see why it shouldn't work. The problem is, when I first load up the guestbook page, I type in something in the boxes, then click submit. Nothing appears, I then try again with different text. And the text I typed in before appears. Try it out on the url at the bottom

Here is the php code:

[php]
<?php
if (($username != "") && ($username != ""))
{
$gbFile = fopen("includes/guestbook.txt", "a");
fputs($gbFile, "
Name: $username<br />
Email: $usermail<br />
Comments: $usercomments<hr />");
}
?>

<?php
include "includes/guestbook.txt";
?>
[/php]

And here is the form html:

Code:
<form action="index.php?page=guestbook" method="post"> <p> <h2>Name:</h2> <input class="contactForm" type="text" name="username" value="name"> </p> <p> <h2>Email:</h2> <input class="contactForm" type="text" name="usermail" value="name"> </p> <p> <h2>Comment:</h2> <textarea class="contactForm" rows="3" name="usercomments">name</textarea> </p> <input type="submit" value="Submit Form"> </form>
The url for all this is here.

Hope you guys can help,
Oliver
     
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Feb 7, 2004, 02:57 PM
 
Have you tried closing the file pointer? After writing your data try:

fclose($gbFile);
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Feb 7, 2004, 03:38 PM
 
I tried what you said, closing the file:

[php]
<?php
if (($username != "") && ($username != ""))
{
$gbFile = fopen("includes/guestbook.txt", "a");
fputs($gbFile, "
Name: $username<br />
Email: $usermail<br />
Comments: $usercomments<hr />");
fclose($gbFile);
}
?>
[/php]

But it seems that its still the same problem, any other ideas?

Thanks,
Oliver
     
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Feb 7, 2004, 03:57 PM
 
What you're doing is taking the data from the user and then writing it to the file, and then including the file. Perhaps there would be less overhead if you put the data you wanted to save into a variable like $myData. Save myData to the file.. close the file and then echo $myData. This saves the step of having to read or include the file. Am I understanding this correctly?
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Feb 7, 2004, 04:16 PM
 
Originally posted by Synotic:
What you're doing is taking the data from the user and then writing it to the file, and then including the file. Perhaps there would be less overhead if you put the data you wanted to save into a variable like $myData. Save myData to the file.. close the file and then echo $myData. This saves the step of having to read or include the file. Am I understanding this correctly?
I'm a bit new to PHP (really new infact ) is there any example code for something like this? Is this why my guestbook isn't working?

Thanks,
Oliver
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Feb 7, 2004, 04:28 PM
 
I think I'm close to figuring this out. So I have a blank text file, I go into my form and fill it in and click submit, it shows nothing. I reload the .txt file in a browser and I can see what I typed in. I then refresh the guestbook and see my entry is there. So I'm wondering why isn't it showing it on the page straight away after I've submitted it. I have had an idea to maybe, when they click submit, transfer the user to a Thank You page and then letting them click back. How could this be done? (novice at forms as well as php too ).
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Feb 7, 2004, 05:42 PM
 
Right, to load a different page after clicking submit, I used this code:

[php]
<?php
$redirecturl ="index.php?page=thankyou";
if (($username != "") && ($username != ""))
{
$gbFile = fopen("includes/guestbook.txt", "a");
fputs($gbFile, "
Name: $username<br />
Email: $usermail<br />
Comments: $usercomments<hr />");
fclose($gbFile);
}
header("Location: $redirecturl");
?>
[/php]

But I seem to get the error:

Warning: Cannot modify header information - headers already sent by (output started at /home/.padua/talacia/talacia.com/puertobanus/index.php:9) in /home/.padua/talacia/talacia.com/puertobanus/includes/guestbook.inc on line 42

I have no idea what this means except that it may have something to do with me using index.php?page=guestbook?

Any ideas?
Oliver
     
Senior User
Join Date: Dec 2002
Status: Offline
Reply With Quote
Feb 7, 2004, 06:00 PM
 
You have to set header info before you can do anything else. If the thank you page is always in the same place, just use a META redirect with HTML.
Travis Sanderson
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:57 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2