SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Tell a Friend : Building the Page

We form entry, form processing, and sending of the e-mail are all accomplished by the same page. The first step is to set-up a few default values:

' Your web-site
const defaultSite = "http://www.SiteExperts.com"
' The name of your site
const siteName = "SiteExperts.com"
' An e-mail address to report abuse of this features 
' (eg., users spamming others)
const abuseAccount = "abuse@SiteExperts.com"
' A text "ad" to append to the e-mail
const footer = "The SiteExperts.com Community is a great place to join with _
 your peers and learn how to build and develop web-sites. Be sure to visit _
 us today!"

After setting the defaults, the rest of the script is ready to go. We walk you through the script below.

We are using the same page to create as well as process the form. We distinguish between the user first entering the page and the user requesting the mail to be sent by looking for a form submission. If no form submission has been made, we look at the querystring for the URL to the recommended page:

' Get default form values
' These are all empty unless the user submitted the form
str_Error = ""
src_page = request.form("p")
src_name = request.form("src_name")
src_email = request.form("src_email")
dest_name = request.form("dest_name")
dest_email = request.form("dest_email")
str_message = request.form("str_message")
src_send = request.form("src_send")

' This is called to initialize the page.
if request.queryString("p")<>"" then
	src_page = request.queryString("p")
end if

After we have the page name, we verify the page exists on our server. We do this test both when initially displaying the form and when the form is submitted. This ensures that noone is trying to sneak a different page through the system:

if objFSO.FileExists(server.mappath(src_page)) Then
	src_page = defaultSite & "/" & src_page
else	
	src_page = defaultSite 
end if

' The encoded URL
escPage = server.urlEncode(src_page)

When first entering this page the user needs to fill out a form telling us who to send the recommendation to. This form is also displayed if there is an error in the user's original submission. When reviewing the code below, notice that we initialize each form value. If the user omits a required field, this form is redisplayed including all the information the user did enter.

<%
if (request.form("p")="" or str_Error<>"") then
%>
<%=str_Error%>
<FORM METHOD=post>
<INPUT TYPE=hidden NAME="p" VALUE="<%=escPage%>">

Recommend the following page:<BR>
<%=src_page%><BR>
<TABLE>
<TR><TD>*Your Name:</TD>
<TD><INPUT TYPE=name NAME="src_name" VALUE="<%=src_name%>"></TD></TR>
<TR><TD>*Your E-Mail Address:</TD>
<TD><INPUT TYPE=text NAME="src_email" VALUE="<%=src_email%>"></TD></TR>
<TR><TD>*Your Friend's Name:</TD>
<TD><INPUT TYPE=text NAME="dest_name" VALUE="<%=dest_name%>"></TD></TR>
<TR><TD>*Your Friend's e-mail Address:</TD>
<TD><INPUT TYPE=text NAME="dest_email" VALUE="<%=dest_email%>"></TD></TR>
<TR><TD>Your Message:</TD>
<TD><TEXTAREA NAME="str_message"><%=str_message%></TEXTAREA></TD></TR>
<TR><TD>Send Myself a Copy:</TD>
<TD><INPUT TYPE=checkbox NAME="src_send" 
<% if src_send<>"" or str_error="" then response.write("CHECKED")%>
>
</TD></TR>
<TR><TD colspan=2><SMALL>* Required Fields</TD></TR>
<TR><TD COLSPAN=2 align=center>
<INPUT TYPE=submit VALUE="Tell my friend!"></TD></TR>
</TABLE>
</FORM>

<%
end if
%>

The last step is to send the recommendation.

nl = chr(13) & chr(10)

if (request.form("p")<>"") then
  if src_name<>"" and src_email<>"" and dest_email<>"" and dest_name<>"" then
    strSafety = nl & "This message was sent to you on behalf of " & src_name & ". " 
    strSafety = strSafety & " If you do not know"& " " & src_name 
    strSafety = strSafety & " please ignore this message or report it to" 
    strSafety = strSafety & " " & abuseAccount & "."
    set objMail = CreateObject("CDONTS.NewMail")
    ' Send the mail from the user
    objMail.from = src_email
    ' Send the mail to the friend
    objMail.To = dest_email
    ' Copy the user
    if (src_send<>"") then objMail.cc = src_email
    objMail.subject = siteName & " Recommendation"
    strBody = dest_name & "," & nl & nl
    if str_Message<>"" then
      strBody = strBody & src_Name & "(" & src_Email & ") says: " 
      strBody = strBody & nl & nl & str_message & nl & nl 
    end if
    strBody = strBody & src_Name & " also suggests you take a look at " & nl 
    strBody = strBody & src_Page & " on " & siteName & "." & nl & nl
    strBody = strBody & strSafety & nl
    strBody = strBody & footer
    objmail.body = strBody
    objMail.send
    set objMail=nothing
    response.write("<P><B>We send your recommendation!</B>")
  else
    str_Error = "<P><B>You did not fill out all required fields.</B>"
  end if
end if

Next we demonstrate this code by letting you recommend this article to a friend. To see this in action try recommending the article to yourself. We will send you the e-mail generated by this script..

Page 1:Tell a Friend
Page 2:Building the Page
Page 3:Recommend this Article
Page 4:All the Code