Hi everyone, I have had great success with those who helped me get my guestbook working in being able to post large amounts of text. Now I have one other problem I'm trying to figure out.

I'm using the perl guestbook from Matt's Script Archive to allow users at my website to post stories to the site. It posts their name, email, the story name, and the entire story. Now the page is getting quite a few stories on it, but the problem is, they all just get posted on after another down the page, making for a lot of scrolling down the page to find a story. I did get my script to post a link with every story that takes you back to the top of the page, but now, it would be nice if the script could also take the name of the story, and in addition to posting the whole story on the page, create a link at the top of the page going to each story.

This is difficult, because the guestbook is basically just posting their stories on top of all the other stories, and pushing the old ones down. How do I get it to also post something in say a table or something at the top of the page? Ok, maybe not in a table, but in some organized manner at the top.
Thanks for you help,
Nate

I've been Looking at your code, and what you are wanting to do is going to be a little bit of work. It's a pain, because, the guestbook is saved as an HTML document, and then parsed and displayed. The reason this is a problem is because we have to set our reference point in the HTML document. If you view the script, you'll see the line that looks for a begin point: if (/<!--begin-->/) {, the <!--begin--> is actually an HTML comment, that is in your HTML document. If you were to load your guestbook in a web browser, right click, and view page source, you would see that line at the top just before the guestbook entries are displayed. That's the key to tell the script, that now begins the entries. I am posting to let you know that I haven't forgotten about you, but that I'm looking at methods that will work effeciently for this script.

Thanks for your reply. I was thinking the same thing. I noticed the reference point, as I had to make sure to put it in my web page to make sure the guestbook would work. I was thinking though, couldn't I just make another custom comment in my webpage, and then use the same code that looks for the other comment, only change the comment it looks for, and have it post after that? That way, the code would run, post the regular guestbook stuff after the first comment, then after that code runs, and would paste a copy of that code beneath it, change the comment to whatever else I specify, and it would go back to the HTML document, and post wherever it finds that code?

Even if it wouldn't work going in that order, I could switch it around, and have it post the link first, then post the guestbook stuff later down. Let me know what you think.
Thanks,
Nate

Well, I know that HTML allows you set labels.... such as:

<A name="section1">Introduction</A>

And Then You use the link such as:

<A href="#section1">Jump To Intro</A>

If they click on the link that says "Jump To Intro" it will jump (move the scroll bar and everything) down to the link with the name "section1". The problem I'm having, is that it looks to me like the cgi never accepts a story "name". For example:

$FORM{'realname'}
$FORM{'username'}
$FORM{'story'}

Ok, We have the persons real name, Their username, and their story (the content). I see no mention of a story "name" or "title" of the story. We can't do it by name or username, because then people can't post more than once, or it will throw things out of whack. Also, Keep in mind, that the CGI does NOT re-write the page. It only inserts itself into the top between <!--begin--> and the last posted entry. That means if you were to add a table or something in there, it would keep adding tables. You wouldn't be able to re-write the table, but you could probably add to it.

I personally don't care for matt's guestbook perl script. No offense, but I don't like his method. The problem with re-writing it to work differently, or to completly re-write one for your site specifically, would mean an awful lot of copy and pasting, and probably wouldn't be worth that much time. If we can find a way to get a story name or title, we could probably do it with what we are working with.

I actually do have a label calling the story name, it's just called "storyname". Here, I'll post the code for the whole thing here (but I'll change a couple things just to keep it safe and private):

#!/usr/bin/perl
# Set Variables
$guestbookurl = "http://www.MYSITEURL.com/stories.htm";
$guestbookreal = "/PATH/TO/MY/STORIES/PAGE/stories.htm";
$guestlog = "/PATH/TO/MY/LOG/storyguestlog.html";
$cgiurl = "http://www.royandstellawood.com/cgi-bin/storyguestbook.pl";
$date_command = "/bin/date";

# Set Your Options:
$mail = 1;              # 1 = Yes; 0 = No
$uselog = 0;            # 1 = Yes; 0 = No
$linkmail = 0;          # 1 = Yes; 0 = No
$separator = 1;         # 1 = <hr>; 0 = <p>
$redirection = 0;       # 1 = Yes; 0 = No
$entry_order = 1;       # 1 = Newest entries added first;
                        # 0 = Newest Entries added last.
$remote_mail = 1;       # 1 = Yes; 0 = No
$allow_html = 0;        # 1 = Yes; 0 = No
$line_breaks = 0;	# 1 = Yes; 0 = No
$fromaddress = 'EMAILADDRESS@MYURL.com';
# If you answered 1 to $mail or $remote_mail you will need to fill out 
# these variables below:
$mailprog = '/usr/lib/sendmail';
$recipient = 'EMAILADDRESS@MYURL.com';

# Done
##############################################################################

# Get the Date for Entry
$date = `$date_command +"%A, %B %d, %Y"`; chop($date);
$shortdate = `$date_command +"%D %T %Z"`; chop($shortdate);

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
   ($name, $value) = split(/=/, $pair);

   # Un-Webify plus signs and %-encoding
   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $value =~ s/<!--(.|\n)*-->//g;

   if ($allow_html != 1) {
      $value =~ s/<([^>]|\n)*>//g;
   }

   $FORM{$name} = $value;
}

# Print the Blank Response Subroutines
#&no_comments unless $FORM{'comments'};
#&no_name unless $FORM{'realname'};

# Begin the Editing of the Guestbook File
open (FILE,"$guestbookreal") || die "Can't Open $guestbookreal: $!\n";
@LINES=<FILE>;
close(FILE);
$SIZE=@LINES;

# Open Link File to Output
open (GUEST,">$guestbookreal") || die "Can't Open $guestbookreal: $!\n";

for ($i=0;$i<=$SIZE;$i++) {
   $_=$LINES[$i];
   if (/<!--begin-->/) { 

     if ($entry_order eq '1') {
         print GUEST "<!--begin-->\n";
      }
   
      
if ( $FORM{'storyname'} ){
         print GUEST "<h1><b><center>$FORM{'storyname'}</center></h1></b><br>\n";
      }
      print GUEST "<a href=\"#Top\"><u>(return to top)</u></a><br>\n";

      if ($FORM{'url'}) {
         print GUEST "<a href=\"$FORM{'url'}\">$FORM{'realname'}</a>";
      }
      else {
         print GUEST "Submitted by: <b>$FORM{'realname'}</b><br>";
      }

      if ( $FORM{'username'} ){
         if ($linkmail eq '1') {
            print GUEST " \<<a href=\"mailto:$FORM{'username'}\">";
            print GUEST "$FORM{'username'}</a>\>";
         }
         else {
            print GUEST "Email: <b>$FORM{'username'}</b>";
         }
      }

      print GUEST "<br>\n";
      print GUEST "Date Submitted: $date<br>\n";

      


      if ( $FORM{'intro'} ){
         print GUEST "Story Introduction: $FORM{'intro'}<p>\n";
      }

         

         print GUEST "<b>Story:</b><br>\n";
         $story=$FORM{'story'};
         $story =~s/\n/<br>/gi;
         print GUEST "$story\n";
      

       print GUEST "<hr>\n\n";
	   
      if ($entry_order eq '0') {
         print GUEST "<!--begin-->\n";
      }

   }
   else {
      print GUEST $_;
   }
}

close (GUEST);


# Log The Entry

if ($uselog eq '1') {
   &log('entry');
}


#########
# Options

# Mail Option
if ($mail eq '1') {
   open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n";

   print MAIL "Reply-to: $FORM{'username'}\n";
   print MAIL "From: $FORM{'username'}\n";
   print MAIL "Subject: New Story Submission\n\n";
   print MAIL "$FORM{'realname'} from email address $FORM{'username'} has posted a story to the web site.\n\n";
   print MAIL "Name: $FORM{'realname'}\n";
   print MAIL "Name of Story: $FORM{'storyname'}\n";
   print MAIL "Introduction to Story: $FORM{'intro'}\n";
   print MAIL "Story:\n";
   print MAIL "$FORM{'story'}\n\n";
   print MAIL "-$date\n";
   print MAIL "------------------------------------------------------\n";

   close (MAIL);
}

if ($remote_mail eq '1' && $FORM{'username'}) {
   open (MAIL, "|$mailprog -t") || die "Can't open $mailprog!\n";

   print MAIL "To: $FORM{'username'}\n";
   print MAIL "From: $fromaddress\n";
   print MAIL "Subject: New Story Submission\n\n";
   print MAIL "You are receiving this email because you submitted a story to the website.\n";
   print MAIL "Below is what you submitted.\n\n";
   print MAIL "Name: $FORM{'realname'}\n";
   print MAIL "Email Address: $FORM{'username'}\n";
   print MAIL "Name of Story: $FORM{'storyname'}\n";
   print MAIL "Story:\n";
   print MAIL "$FORM{'story'}\n\n";
   print MAIL "-$date\n";
   close (MAIL);
}

# Print Out Initial Output Location Heading
if ($redirection eq '1') {
   print "Location: $guestbookurl\n\n";
}
else { 
   &no_redirection;
}



# Log the Entry or Error
sub log {
   $log_type = $_[0];
   open (LOG, ">>$guestlog");
   if ($log_type eq 'entry') {
      print LOG "$ENV{'REMOTE_HOST'} - [$shortdate]<br>\n";
   }
   elsif ($log_type eq 'no_name') {
      print LOG "$ENV{'REMOTE_HOST'} - [$shortdate] - ERR: No Name<br>\n";
   }
   elsif ($log_type eq 'no_comments') {
      print LOG "$ENV{'REMOTE_HOST'} - [$shortdate] - ERR: No ";
      print LOG "Comments<br>\n";
   }
}

# Redirection Option
sub no_redirection {

   # Print Beginning of HTML
   print "Content-Type: text/html\n\n";
   print "<html><head><title><center>Thank you.</center></title></head>\n";
   print "<body bgcolor=\"ece4dd\">\n";
   print "<body><h1><center>Thank you for submitting your story.</h1></center>\n";
   print "<body><Center>A copy of your story has been sent to your email address.</center>\n";
   print "<body><Center>Click <a href=\"/stories.htm\">HERE</a> to return to the stories page.</center>\n";
   print "<body><center>You may have to refresh the page to see your story.\n";
   

   
   # Print End of HTML
   print "<hr>\n";
 
   print "</body></html>\n";

   exit;
}

I know it's long posting all that, but I thought it would be somewhat helpful. Anyway, I figure I can just use the same code for posting the story, but change the comment in the HTML it looks for.

Then once I get it posting there, then couldn't I just name a tag $storylink={'storyname'} or something, and then just have it post somthing like:

print GUEST "<a href=\"#storyname\"><u>$FORM{'storyname'}</u></a><br>\n";

I know the syntax might not be correct, but wouldn't that work?

Then I guess if I can get that one working, then I would have to figure out how to get it to start posting in columns or something, so it doesn't just post this huge list of story names that you have to scroll down to see anyway.

I know this may be too much to do just re-writting matt's Script. But I couldn't find any other scripts out there to do this story posting thing. I found plenty of scripts that would post like a never-ending story type thing, but not like what I'm doing.
Thanks for your help.
Nate

Actually, I think you have it down pat. All you need to do is use the link like you showed above, at each story name, but like:

print GUEST "<a href=\"$FORM{'storyname'}\"><u>$FORM{'storyname'}</u></a><br>\n";

or maybe even:

print GUEST "<a href=\"" . $FORM{'storyname'} . "\"><u>$FORM{'storyname'}</u></a><br>\n";

But you have the concept and idea. Then to make the table at the top with the list of story names will be the only trick. I was thinking you could add it just like the other one, but you would have to have a reference point at the bottom of the table....like, just before </TABLE>, or something like that at the top. Example:

<TABLE>
     <TR>
          <TD>Link to Story 1</TD>
     </TR>
     <TR>
          <TD>Link to Story 2</TD>
     </TR>
     <!--add story here-->
</TABLE>

Then your perl code (near where <!--begin--> is):

if (/<!--add story here-->/) {
     print GUEST "<TR>\n";
     print GUEST "<TD><A HREF=\"#$FORM{'storyname'}\">$FORM{'storyname'}</A></TD>
     print GUEST "</TR>\n";
}

That should work out fairly well. You seem to grasp the idea. Don't forget to add the reference point for the table at the top or whatever you plan to do. Look to me like it should work a charm :)
Let me know how it turns out.

Thanks, your table idea looks like it will work out swell! Now I just need to find the time to play with the code and test it out. I haven't even done anything to it yet. I actually know nothing about Perl, except for what I've learned playing around with Matt's guestbook. It's been fun though. I'm going to play with it for the next couple days, and I'll let you know what I come up with.
Thanks for all your help and ideas!
Nate

it's my pleasure. On Another Note, I have written 3 tutorials, on writing your own CGI's with Perl. Right now it only covers checking if data exists, reading data from the page, and writing data to a file, but I plan to make a few more, and ultimately have it cover a how to build your own guestbook, or something similar. Whenever you have time, it might be beneficial for you to read them.... I've gotten a few requests for more, and a couple of questions sent to me through PM's about them, but they are fairly straight forward and easy to understand. Let me know if I can help you with anything else, and let me know the guestbook turns out for you.

CGI Tutorials:
http://www.daniweb.com/tutorials/forum84.html

Thanks! I'll check into it sometime.

I guess I should look into your tutorial to get the answer to this, but I've been playing around with it, and it's doing something really funny. After several attempts, I've got it working somewhat, right now, I'm just having it post some words up in the top portion, cause if I can get that working, then getting it to post the link is no problem.

However, it will post the first one just fine. Then when I try to do it again, it posts the words, and posts them again. It's like it's posting what it's supposed to, plus a few. I think it has to do with this code that starts it:

for ($i=0;$i<=$SIZE;$i++) {
   $_=$LINES[$i];

But I'm not really sure. Here, I'll post just the part that is posting the story to the page, and you can tell me what you think.

for ($i=0;$i<=$SIZE;$i++) {
   $_=$LINES[$i];
####THIS IS THE PART I ADDED#########################33
if (/<!--links-->/) { 

               print GUEST "TEST LINK<br>\n";
    }

##########I ADDED THE STUFF ABOVE THIS LINE################

   if (/<!--begin-->/) { 

     if ($entry_order eq '1') {
         print GUEST "<!--begin-->\n";
      }
   
      
if ( $FORM{'storyname'} ){
         print GUEST "<h1><b><center>$FORM{'storyname'}</center></h1></b><br>\n";
      }
      print GUEST "<a href=\"#Top\"><u>(return to top)</u></a><br>\n";

      if ($FORM{'url'}) {
         print GUEST "<a href=\"$FORM{'url'}\">$FORM{'realname'}</a>";
      }
      else {
         print GUEST "Submitted by: <b>$FORM{'realname'}</b><br>";
      }

      if ( $FORM{'username'} ){
         if ($linkmail eq '1') {
            print GUEST " \<<a href=\"mailto:$FORM{'username'}\">";
            print GUEST "$FORM{'username'}</a>\>";
         }
         else {
            print GUEST "Email: <b>$FORM{'username'}</b>";
         }
      }

      print GUEST "<br>\n";
      print GUEST "Date Submitted: $date<br>\n";

      


      if ( $FORM{'intro'} ){
         print GUEST "Story Introduction: $FORM{'intro'}<p>\n";
      }

         

         print GUEST "<b>Story:</b><br>\n";
         $story=$FORM{'story'};
         $story =~s/\n/<br>/gi;
         print GUEST "$story\n";
      

       print GUEST "<hr>\n\n";

   
      if ($entry_order eq '0') {
         print GUEST "<!--begin-->\n";
      }

   }
   else {
      print GUEST $_;
   }
}

So like I said, it posts the first one just fine, but it adds to many links after the first one.
Thanks,
nate

The only thing that this code does:

for ($i=0;$i<=$SIZE;$i++) {
   $_=$LINES[$i];
}

is loop through each line of the file, and sets the default variable ($_) to the next line in the file. Basically, it's reading the HTML file line by line, and replacing $_ with the next line after the loop. So, It reads line1, checks to see if it's <!--begin-->, and then does something if it is.... if not, it just reads the next line, and keeps doing so. Every time you see a "print GUEST" it's writing something new to the HTML document. This line here:

else {
      print GUEST $_;

Is actually writing "everything else" to the file. That's how it makes sure not to lose any other posts or anything. It just writes it back to the file. The reason the <!--begin--> exists, is solely so the script knows WHERE to add it's new record in the file. This also means that it doesn't reach the code posted above (the else), because the else only gets called should the line NOT be "<!--begin-->", meaning the stuff in the file doesn't get re-written. Give this a shot:

for ($i=0;$i<=$SIZE;$i++) {
   $_=$LINES[$i];
####THIS IS THE PART I ADDED#########################33
if (/<!--links-->/) { 
	print GUEST "<!--links-->\n";   # /* I Added This */
	print GUEST "TEST LINK<br>\n";
} else { # /* I Added This */

##########I ADDED THE STUFF ABOVE THIS LINE################

   if (/<!--begin-->/) { 

     if ($entry_order eq '1') {
         print GUEST "<!--begin-->\n";
      }
   
      
if ( $FORM{'storyname'} ){
         print GUEST "<h1><b><center>$FORM{'storyname'}</center></h1></b><br>\n";
      }
      print GUEST "<a href=\"#Top\"><u>(return to top)</u></a><br>\n";

      if ($FORM{'url'}) {
         print GUEST "<a href=\"$FORM{'url'}\">$FORM{'realname'}</a>";
      }
      else {
         print GUEST "Submitted by: <b>$FORM{'realname'}</b><br>";
      }

      if ( $FORM{'username'} ){
         if ($linkmail eq '1') {
            print GUEST " \<<a href=\"mailto:$FORM{'username'}\">";
            print GUEST "$FORM{'username'}</a>\>";
         }
         else {
            print GUEST "Email: <b>$FORM{'username'}</b>";
         }
      }

      print GUEST "<br>\n";
      print GUEST "Date Submitted: $date<br>\n";

      


      if ( $FORM{'intro'} ){
         print GUEST "Story Introduction: $FORM{'intro'}<p>\n";
      }

         

         print GUEST "<b>Story:</b><br>\n";
         $story=$FORM{'story'};
         $story =~s/\n/<br>/gi;
         print GUEST "$story\n";
      

       print GUEST "<hr>\n\n";

   
      if ($entry_order eq '0') {
         print GUEST "<!--begin-->\n";
      }

   }
   else {
      print GUEST $_;
   }
}
} # /* I Added This */
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.