Database-Driven Guestbook

Introduction

Guestbooks allow you to collect and format comments from web site visitors for display on your web site. Database-driven guestbook entries are collected via a form, added to the guestbook database, then displayed from the guestbook database. You may format your guestbook entries with any text and HTML that you like.

The following is an example of a simple comment guestbook.

MGI Tags

Steps

  1. Create a form to collect guestbook information.
  2. Create a guestbook processing page and open it in a text editor.
  3. Insert the mgiGuestbookDB tag in submit mode.
  4. Save the guestbook processing page.
  5. Create a guestbook display page and open it in a text editor.
  6. Insert the mgiGuestbookDB tag in display mode.
  7. FTP the form and guestbook pages to the web server running MGI.
  8. View the guestbook form and submit an entry.


Step 1: Create a form to collect guestbook information.

Create a guestbook form with form elements. In this example, the guestbook form consists of text fields for the visitor's Name, Location and Comments. Name each form element uniquely. Enclose all form elements with HTML <FORM> tags and post the form to the guestbook page (guestbook.mgi).
 
This is an example guestbook form.
<form action="guestbook.mgi" method="post">

<center>
<h2>Sign Our Guestbook</h2>

<p><table cellpadding="3" cellspacing="0">

<tr><td>Name:</td>
<td><input type="text" name="Name" size="30">
</td></tr>

<tr><td>Location (City, State):</td>
<td><input type="text" name="Location" size="30">
</td></tr>

<tr><td>Comments:</td>
<td><textarea name="Comments" rows="5" cols="30">
</textarea></td></tr>

<tr><td>&nbsp;</td>
<td><input type="submit" value="Post Comments">
</td></tr>

</table>
</center>

</form>

Step 2: Create a guestbook processing page and open it in a text editor.

Create a page named "process.mgi" to submit the entry to the guestbook database. Open the page in a text editing program that allows you to view and modify the HTML and code of the page.

Step 3: Insert the mgiGuestbookDB tag in submit mode.

Enter the beginning mgiGuestbook tag, mode parameter, name parameter and ending mgiGuestbookDB tag. In the mode parameter, enter "submit". In the name parameter, enter the case-sensitive name of the guestbook.
 
In the body of the mgiGuestbookDB tags, enter text, HTML, and mgiPostArgument tags for form information.
 
In this example the visitor is immediately redirected to the guestbook display page via the mgiRedirect tag.
 
The following is an example guestbook processing page.
<mgiGuestbookDB name="Comments" 
mode="submit" resultsPerPage="10" 
page={mgiGet name="PageToDisplay"} 
resultVariableName="GBResults">

<table width="400" cellpadding="3" cellspacing="0">
<tr>
<td bgcolor="#99cc99"><b><mgiPostArgument name="Name"> of 
<mgiPostArgument name="Location"></b></td>
</tr>
<tr>
<td><font color="#660099">
<mgiPostArgument name="Comments"></font></td>
</tr>
</table>

</mgiGuestbookDB>

<mgiRedirect url="guestbook.mgi">

Step 4: Save the guestbook processing page.

Save the changes you have made to the guestbook processing page.

Step 5: Create a guestbook display page and open it in a text editor.

Create a page named "guestbook.mgi" to display entries from the guestbook database. Open the page in a text editing program that allows you to view and modify the HTML and code of the page.

Step 6: Insert the mgiGuestbookDB tag in display mode.

Enter the beginning mgiGuestbook tag, mode parameter, name parameter, resultsPerPage parameter, page parameter, resultVariableName parameter, and ending mgiGuestbookDB tag.
 
In the mode parameter, enter "display". In the name parameter, enter the case-sensitive name of the guestbook. In the resultsPerPage parameter, enter the number of guestbook results to display per page. If the resultsPerPage parameter is not included, 25 results will display by default.
 
If it is possible for the number of guestbook entries to exceed the value listed in the resultsPerPage parameter, you must construct dynamic "Previous" and "Next" buttons to allow visitors to browse through different sets of guestbook entries. Below the mgiGuestbookDB tags, enter a hidden input for the value of the previous page of entries and the value of the next page of entries using the result variables (ResultVariableName_PrevPage and ResultVariableName_NextPage). Below the hidden inputs, display a "Previous" button if you are not currently displaying the first set of search results and display a "Next" button if there is more than one page of results available.
 
Above the mgiGuesbookDB tags, perform a conditional comparison to determine which set of entries should be displayed. If the visitor selected the "Previous" button during a search, then display the previous set of entries. If the visitor selected the "Next" button during a search, then display the next set of entries. Otherwise, display the first set of results. Set the page value in a variable and embed that variable in the page parameter of the mgiGuestbookDB tag.
 
In order for the "Previous" and "Next" buttons to function, enter HTML <FORM> tags. The action of the form tag should be the name of the guestbook page (guestbook.mgi) and the method should be "post".
 
The following is an example guestbook display page.
<mgiComment>
Determine which page of entries to display
</mgiComment>

<mgiSet name="PageToDisplay" defaultValue="1">
  <mgiInlineIf lhs={mgiPostArgument name="ResultsSet"} 
  relationship="equals" rhs="Prev" 
  then={mgiPostArgument name="PrevPage"}>
  
  <mgiInlineIf lhs={mgiPostArgument name="ResultsSet"} 
  relationship="equals" rhs="Next" 
  then={mgiPostArgument name="NextPage"}>
</mgiSet>

<mgiComment>
Submit and Display Entries
</mgiComment>

<mgiGuestbookDB name="Comments" 
mode="display" resultsPerPage="10" 
page={mgiGet name="PageToDisplay"} 
resultVariableName="GBResults">
</mgiGuestbookDB>

<form action="guestbook.mgi" method="post">

<mgiComment>
Display dynamic Previous and Next Buttons
</mgiComment>

<input type="hidden" name="PrevPage" 
value={mgiGet name="GBResults_PrevPage"}>

<input type="hidden" name="NextPage" 
value={mgiGet name="GBResults_NextPage"}>

<mgiIf lhs={mgiGet name="GBResults_Page"} 
relationship="greaterThan" rhs="1">
<input type="submit" name="ResultsSet" value="Prev">
</mgiIf>

<mgiIf lhs={mgiGet name="GBResults_NextPage"} 
relationship="greaterThan" rhs="0">
<input type="submit" name="ResultsSet" value="Next">
</mgiIf>
	
</center>
	
</form>

Step 7: Save the guestbook display page.

Save the changes you have made to the guestbook display page.

Step 8: FTP the form and guestbook pages to the web server running MGI.

Upload the form and guestbook pages from your local computer to the web server using an FTP program.

Step 9: View the guestbook form and submit an entry.

View the guestbook form in a web browser. Complete the guestbook form and submit an entry. On the guestbook processing page your entry is submitted to the guestbook database and you are redirected to the guestbook display page. Newer guestbook entries appear before older guestbook entries on the display page..

Sandra Clemmons of Maryland
Your products are the best. Keep up the good work. I am looking forward to the new line that is due out in March.

Pete Wilson of Atlanta, Georgia
Just wanted to say that my last order was a hit at my party. That is why today I am ordering some more. Also could you please tell me when will be the latest date I can order for xmas holidays... Have yourself a great day!


[Return to the Guestbooks Menu]


[Understanding MGI Menu] [Using MGI Menu] [Referencing MGI Menu]


[MGI Guides Main Menu] [User Guide Main Menu]